Re: float

Thomas Weber <[email protected]> Tue, 11 Jan 2011 09:57:15 +0100
Newsgroups org.kernel.vger.linux-c-programming
Message-ID <[email protected]>
Am 11.01.2011 09:34, schrieb ratheesh k:
> On Tue, Jan 11, 2011 at 1:31 PM, Thomas Weber
> <[email protected]> wrote:
>> Okay, now I got it.
>>
>> Look for IEEE754.
>>
>> 0.1 is in double precision 0.10000000149011612
>>
>> Thomas
>>
> 
> Is it possible to print the correct "printf" statement by typecasting
> some of operations or  data types. I tried with no success.
> 
> -Ratheesh

Try something with precision and typecast float to double

printf("f:%.10f is 1.0 \n",(double) f);


Thomas

> 
> 
>> Am 11.01.2011 08:55, schrieb Uriel Corfa:
>>> I'd say : rounding errors. Floating point goes in mysterious ways.
>>>
>>> On Tue, Jan 11, 2011 at 8:38 AM, ratheesh k <[email protected]> wrote:
>>>> I could not understand  why it getting printed like this. Could any
>>>> body tell me.
>>>>
>>>> #include <stdio.h>
>>>>
>>>> int main()
>>>> {
>>>>        float f=0.0f;
>>>>        int i;
>>>>
>>>>        for(i=0;i<10;i++)
>>>>                f = f + 0.1f;
>>>>
>>>>        if(f == 1.0f)
>>>>                printf("f is 1.0 \n");
>>>>        else
>>>>                printf("f is NOT 1.0\n");
>>>>
>>>>        return 0;
>>>> }
>>>
>>