473,761 Members | 10,684 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1688
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.co m (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.co m (Ketan Parikh) wrote in message news:<ba******* *************** ****@posting.go ogle.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
3679
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. However, they generate slightly different results for floating point numbers. Are they really supposed to generate exactly the same results? I guess so because both platforms are supposed to be IEEE floating point standard (754?) compliant. ...
687
23646
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 believe that. in pieces of the application where speed really matters you can still use "normal" functions or even static methods which is basically the same. in C there arent the simplest things present like constants, each struct and enum...
24
2249
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 floating point when dealing with dollars and cents.
4
2837
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 would like your expert advise before I publish any serious nonsense. Any comments are welcome, style, organization, hard errors, etc.
10
2411
by: chanma | last post by:
code1:var x=0xf0000000; alert(x); output:4026531840 code2:var b=0xf<<28; alert(b); output:-268435456
32
4116
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 ); } int main() { std::deque<double> pt ;
18
1917
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 fractions with integers, while it should be more fairly compared to floats. Arguments against: - When I use the result of / as a sequence index, it's usually an error which should not be hidden by making the program working for some data, since it...
135
4318
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
5674
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
9521
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9333
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10107
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8768
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7324
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5214
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3863
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3442
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.