473,395 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

A Question about floating point represanation

Hey everybody,

I am new to this group. I am from India.

Consider the following code.

main()
{
float k=0.7;
if (k < 0.7)
printf("Hey");
else
printf("Hello");
}

the o/p of the program is: "Hello" why?

I have tried also with 0.1,0.2 ...0.9

but only in 0.7 and 0.9 case i have got o/p as "Hello"
and in all other cases i have got o/p as "Hey".

so please give me reason about this answer?

I would be very thankfull to u.

Have a nice time

Jay Hind.
Nov 13 '05 #1
3 1664
Ketan Parikh wrote:
....
float k=0.7;
if (k < 0.7)
printf("Hey");
else
printf("Hello");
}

the o/p of the program is: "Hello" why?


See http://www.eskimo.com/~scs/C-faq/s14.html,
especially questions 14.1 and 14.5.

Jirka

Nov 13 '05 #2
In article <ba**************************@posting.google.com >,
ke***@parikh.com (Ketan Parikh) wrote:
Hey everybody,

I am new to this group. I am from India.

Consider the following code.

main()
{
float k=0.7;
if (k < 0.7)
printf("Hey");
else
printf("Hello");
}

the o/p of the program is: "Hello" why?

I have tried also with 0.1,0.2 ...0.9

but only in 0.7 and 0.9 case i have got o/p as "Hello"
and in all other cases i have got o/p as "Hey".

so please give me reason about this answer?


When you write 0.7 you get a floating-point value in double precision
that is very close to the real number 0.7; the difference is probably
much less than 1e-15.

When you write float k = 0.7 then you get a floating-point value in
single precision that is relatively close to the real number 0.7; the
difference is probably something around 1e-7. The double precision value
gets rounded to much lower single precision.

When a number gets rounded from double to float, then some values get
rounded up, some values get rounded up. There are some rare values that
stay unchanged: Most likely your computer uses binary floating point,
and that means all small multiples of powers of two will stay unchanged,
for example 0.5.

Seems that on your computer, 0.7 and 0.9 get rounded down, everything
else except 0.5 gets rounded up, and 0.5 stays unchanged. (Is it a big
coincidence that only 2 values get rounded down and six get rounded up?
No, because 0.1, 0.2, 0.4 and 0.8 must behave the same on a computer
using binary floating point, and so must 0.3 and 0.6, because there is
factor of 2 between them).
Nov 13 '05 #3
k<0.7 is wrong. k =0.7 :))

ke***@parikh.com (Ketan Parikh) wrote in message news:<ba**************************@posting.google. com>...
Hey everybody,

I am new to this group. I am from India.

Consider the following code.

main()
{
float k=0.7;
if (k < 0.7)
printf("Hey");
else
printf("Hello");
}

the o/p of the program is: "Hello" why?

I have tried also with 0.1,0.2 ...0.9

but only in 0.7 and 0.9 case i have got o/p as "Hello"
and in all other cases i have got o/p as "Hey".

so please give me reason about this answer?

I would be very thankfull to u.

Have a nice time

Jay Hind.

Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: JS | last post by:
We have the same floating point intensive C++ program that runs on Windows on Intel chip and on Sun Solaris on SPARC chips. The program reads the exactly the same input files on the two platforms....
687
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't...
24
by: j0mbolar | last post by:
C supports single precision floating point and double precision floating point but does it support fixed floating point? i've read that fixed floating point is more accurate than single precision...
4
by: jacob navia | last post by:
Hi people I continue to work in the tutorial for lcc-win32, and started to try to explain the floating point flags. Here is the relevant part of the tutorial. Since it is a difficult part, I...
10
by: chanma | last post by:
code1:var x=0xf0000000; alert(x); output:4026531840 code2:var b=0xf<<28; alert(b); output:-268435456
32
by: ma740988 | last post by:
template <class T> inline bool isEqual( const T& a, const T& b, const T epsilon = std::numeric_limits<T>::epsilon() ) { const T diff = a - b; return ( diff <= epsilon ) && ( diff >= -epsilon );...
18
by: Lie | last post by:
I'm very surprised actually, to see that Python rejected the use of fractional/rational numbers. However, when I read the PEP, I know exactly why the proposal was rejected: People compared...
135
by: robinsiebler | last post by:
I've never had any call to use floating point numbers and now that I want to, I can't! *** Python 2.5.1 (r251:54863, May 1 2007, 17:47:05) on win32. *** 0.29999999999999999 0.29999999999999999
160
by: raphfrk | last post by:
Is this valid? int a; void *b; b = (void *)a; // b points to a b += 5*sizeof(*a); // b points to a a = 100;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.