-2

Okay i have a simple question . In my adventure i seek the largest numbers can hold in data types and i was trying things like long int , doubles and floats etc.

But in the simplest assigns such as Float x = 12345789 , it gives me 123456792 as a output . Here's the code

#include <stdio.h>


int main()
{
 int x = 1234567891 ;
long int y = 9034567891234567899;
long long int  z = 9034567891234567891;
float t = 123456789 ;
printf("%i \n%li \n%lli \n%f \n ",x,y,z,t);
}

and the output im getting is

1234567891
9034567891234567899
9034567891234567891
123456792.000000

im coding on a linux and using gcc. What could be the problem ?

For clearity , if you give a higher number like

float t = 123456789123456789

it will get the first 9 right but somekind of rounding in last numbers where it should not .

1234567890519087104.000000

İ could have understand it if i was working beyond 0 like 0.00123 but its just straight on integers just to find out limits of float.

  • 2
    Where is the problem? `float` has a limited precision, so it can't represent every number. I think it is about 7 or 8 significant digits (base 10). – Osiris Dec 04 '18 at 20:35
  • 4
    Possible duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Tim Randall Dec 04 '18 at 20:37
  • @TimRandall Not really a duplicate, but the two links should answer the question. – Martin Rosenau Dec 04 '18 at 20:38
  • yeah i kinda understand it if it was something like 0.00000123 and i could just say its the math of floats but its just a straight forward integer. is it just because , float **only** gets the %70 of the digits right ? – M.Karavural Dec 04 '18 at 20:52
  • 1
    `float`s do not have an absolute error, they have a relative one. That means: `123456789 = 1.23456789 * 10^8`, if you take 7 significant numbers you get `1.234567 * 10^8`. To get the exact result you have to do the calculations in binary of course. – Osiris Dec 04 '18 at 20:55
  • Check your question; I can see why assigning 12345789 would not have resulted in 12345789, but it would not result in 123456792, and you actual example does not indicate that. You have one too many digits at least in the second value. – Clifford Dec 04 '18 at 21:10
  • See already: https://stackoverflow.com/questions/3793838/which-is-the-first-integer-that-an-ieee-754-float-is-incapable-of-representing-e – Clifford Dec 04 '18 at 21:40
  • @Clifford oh sorry i wrote it wrong. – M.Karavural Dec 04 '18 at 22:24
  • @Osiris Oh ! now i understand the thing after the answer i got , thank you – M.Karavural Dec 04 '18 at 22:26

2 Answers2

0

    Value:                          123456789
    Hexadecimal representation:     0x4ceb79a3
    Binary representation:          01001100111010110111100110100011
                                    sign    (0)                      : +1
                                    exponent(10011001)               : 2^26
                                    mantissa(11010110111100110100011): 1.8396495580673218
    Value actually stored in float: 1.8396495580673218 * 2^26 = 123456792
    Error due to conversion:        3 

float_converter_image


int main()
{
    float t = 123456789;
}

main:
        push    rbp
        mov     rbp, rsp
        movss   xmm0, DWORD PTR .LC0[rip]
        movss   DWORD PTR [rbp-4], xmm0
        mov     eax, 0
        pop     rbp
        ret
.LC0:
        .long   1290500515     //(0x4CEB79A3)

compiler_explorer_image

  • For your adventure seeking the largest numbers of each data types, I guess your can explore standard header files such as float.h and limits.h.
T.Tom
  • 16
  • 1
0

To find the largest contiguous integer value that can be round-tripped from integer to float to integer, the following experiment could be used:

#include <stdio.h>

int main()
{
    long i = 0 ;
    float fint = 0 ;

    while( i == (long)fint )
    {
        i++ ;
        fint = (float)i ;
    }

    printf( "Largest integer representable exactly by float = %ld\n", i - 1 ) ;

    return 0;
}

However the experiment is largely unnecessary, since the value is predictably 224 since 23 is the number of bits in the float mantissa.

Clifford
  • 88,407
  • 13
  • 85
  • 165