float

S

santosh

"]
float a=0.7;
if(0.7>a)
printf("Hi");
else
printf("Hello");


output is Hi
Why?[/QUOTE]

Because binary floating point representation cannot store most values
accurately. We had a big thread about this recently,
titled "sin(M_PI)".

Firstly read the questions and answers at:

<http://c-faq.com/fp/index.html>

I also suggest that you go over the entire FAQ. Most beginner
questions are answered there.

Also Google for "Goldberg floating point", (without quotes). It's a
classic paper on it's subject.
 
D

Duncan Muirhead

float a=0.7;
if(0.7>a)
printf("Hi");
else
printf("Hello");


output is Hi
Why?
This program:
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char** argv)
{
float a = 0.7f;
double b = 0.7;
printf( "0.7f %s a\n", 0.7f != a ? "!=" : "==");
printf( "0.7 %s a\n", 0.7 != a ? "!=" : "==");
printf( "0.7 %s b\n", 0.7 != b ? "!=" : "==");
return EXIT_SUCCESS;
}
prints
0.7f == a
0.7 != a
0.7 == b

The point is that 0.7 on its own is a double. so in your fragment there
were two conversions. In "float a = 0.7;" the double 0.7 was converted to
a float. Then in "if(0.7>a)" the float value a was converted to a double.
So in effect you are asking if (double)(float)0.7 is the same as 0.7.
Since 0.7 cannot be expressed exactly as a float the answer is no.
 
T

Tim Prince

bhawna said:
float a=0.7;
if(0.7>a)
printf("Hi");
else
printf("Hello");


output is Hi
Why?
You've mixed float and double. Did you forget to write 0.7f ?
 
C

Chip Coldwell

bhawna great said:
float a=0.7;
if(0.7>a)
printf("Hi");
else
printf("Hello");


output is Hi
Why?

Actually, the answer is deep. Mostly the problem is that not all
decimal fractions have exact binary representations. For numbers that
do, you will get the expected results. Try again, setting "a" equal
to

0.5 = 1/2
0.25 = 1/4
0.125 = 1/8
0.75 = 3/4
0.625 = 5/8
0.0625 = 1/16
0.6875 = 22/32

and you'll see that it works as expected. All of these numbers have
exact representations as binary fractions (denominator a power of
two), whereas 0.7 doesn't. The closest floating point number to 0.7
is represented as 0x3F333333 in IEEE 754 single precision,

3f333333 is 0011 1111 0011 0011 0011 0011 0011 0011 in binary
seee eeee emmm mmmm mmmm mmmm mmmm mmmm

s = 0 (sign is positive)
e = 01111110 = 0x7e (exponent is -1 -- the exponent bias is 127)
m = (1)011 0011 0011 0011 0011 0011 = 0x00b33333
(mantissa is 11744051 * 2**(-23))

So the exact value is 11744051/16777216 = 0.699999988079071044921875

Now, on an architecture such as the POWER6 CPU from IBM that supports
decimal floating point (as opposed to binary), you could in principle
represent decimal literals exactly.

Chip
 
B

Barry

Chip Coldwell said:
Actually, the answer is deep. Mostly the problem is that not all
decimal fractions have exact binary representations. For numbers that
do, you will get the expected results. Try again, setting "a" equal
to

0.5 = 1/2
0.25 = 1/4
0.125 = 1/8
0.75 = 3/4
0.625 = 5/8
0.0625 = 1/16
0.6875 = 22/32

and you'll see that it works as expected. All of these numbers have
exact representations as binary fractions (denominator a power of
two), whereas 0.7 doesn't. The closest floating point number to 0.7
is represented as 0x3F333333 in IEEE 754 single precision,

3f333333 is 0011 1111 0011 0011 0011 0011 0011 0011 in binary
seee eeee emmm mmmm mmmm mmmm mmmm mmmm

s = 0 (sign is positive)
e = 01111110 = 0x7e (exponent is -1 -- the exponent bias is 127)
m = (1)011 0011 0011 0011 0011 0011 = 0x00b33333
(mantissa is 11744051 * 2**(-23))

So the exact value is 11744051/16777216 = 0.699999988079071044921875

Now, on an architecture such as the POWER6 CPU from IBM that supports
decimal floating point (as opposed to binary), you could in principle
represent decimal literals exactly.

Chip

If not in binary, how does the power6 store information?
 
D

ditiem

float a=0.7;
if(0.7>a)
printf("Hi");
else
printf("Hello");

output is Hi
Why?

Because you wroted it badly. When working with float NUMBERS must end
in f:

float a = 0.7f ;
....
if ( 0.7f > a )
....

maybe you would like to run this:

float b = 0.7, c=0.7f ;

printf( "\n%X %X\n", b,c) ;
printf( "\n%f %f\n", b,c) ;

Cheers,

PS: Your problem was converting from double (0.7 instead of 0.7f) to
float.
 
C

Chip Coldwell

Barry said:
If not in binary, how does the power6 store information?

The POWER6 CPU does store information in binary; however, it has a
floating-point unit that is capable of doing computations in decimal
arithmetic. This is nothing new; IBM mainframes and everyday pocket
calculators have done floating point in decimal for a very long time.

Folks in the financial sector worry a lot about the fact that numbers
such as 0.1 have no exact binary representation. In binary, one tenth
is an infinitely repeating number

0.00011001100110011001100.... =
1/16 + 1/32 + 1/256 + 1/512 + 1/8192 + 1/16384 + ...

You can see that the series converges to 0.1, but any sum of a finite
part of it (such as a binary floating point number that fits in a
four-byte width) is not exactly 0.1. That is the hazard of making
radix conversions. Most people in the scientific and engineering
communities don't care; the decimal numbers they are working with are
already only approximations that contain errors. However, in the
financial community, the numbers are exact, and so they don't tolerate
radix conversion errors. Prior to the POWER6, most finicial software
that didn't run on mainframes did it's floating point arithmetic
entirely in software so as to avoid converting decimal numbers to a
binary radix.

Chip
 
K

Keith Thompson

Chip Coldwell said:
The POWER6 CPU does store information in binary; however, it has a
floating-point unit that is capable of doing computations in decimal
arithmetic. This is nothing new; IBM mainframes and everyday pocket
calculators have done floating point in decimal for a very long time.
[...]

And computers that support decimal arithmetic usually use a format
called binary-coded decimal, or BCD. Each decimal digit is
represented by a group of 4 bits.

This is relatively inefficient; 4 bits could store any of 16 values,
but 6 of those values are unused. If you do the math, it's only using
about 83% of the available information (a very large number can be
represented in binary in 83% of the bits required by a BCD
representation).

Another more recent encoding uses groups of 10 bits to store 3 decimal
digits. 10 bits could store any of 1024 values; only 24 of them are
unused, so this is substantially more efficient than old-style BCD,
since 1000 is proportionally closer to 1024 than 10 is to 16. 10-bit
decimal uses more than 99.6% of the available information. (This
calculation ignores some mathematical glitches caused by using an
exponent with a larger base, as well as the added difficulty of
implementing this in hardware.)

Note that a C implementation is free to use this kind of thing for
floating-point types, but not for integer types, since integers are
required to use binary notation. But there's less need for this kind
of thing for integers, since they can't represent fractions anyway. A
decimal integer type would make dividing by 10 more efficient (it
would just be a shift), but it doesn't make anything possible that's
not already possible in plain binary. Decimal floating-point, on the
other hand, makes it possible to represent 0.1 exactly, something that
you just can't do with binary floating-point. For some applications,
it's worth the cost.
 
M

Mark McIntyre

float a = 0.7f ;
...
if ( 0.7f > a )

This isn't guaranteed to work in the general case where a is the
result of a calculation - even if /mathematically/ the result of the
calculation is 0.7 precisely. Comparing floating point numbers is
almost always a mistake.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,237
Latest member
AvivMNS

Latest Threads

Top