LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-17-2004, 04:02 AM   #1
vinay_s_s
Member
 
Registered: Jul 2003
Posts: 659

Rep: Reputation: 30
Strange C program -- urgent!


Strange problems here I have mentioned 2 programs which are essentialy same logically but differ by output. Can anyone help me by explaining whats going on ?
Code:
main()
{
  float a=0.7;
  if(a<0.7)
    printf("C\n");
  else 
    printf("C++\n");
}
Output is :
C
--------------------------------------
Code:
main()
{
  float a=0.7;
  if(a<0.7f)
    printf("C\n");
  else 
    printf("C++\n");
}
Output is:
C++
 
Old 02-17-2004, 04:31 AM   #2
Marius2
Member
 
Registered: Jan 2004
Location: Munich
Distribution: SuSE 9.2, 10.2, 10.3, knoppix
Posts: 276

Rep: Reputation: 31
1. What do you see if you insert the printf below?
main()
{
float a=0.7;

//Insert this:
printf("a=%10.10g\n",a);
if(a<0.7f)
printf("C,a=%d\n");
else
printf("C++,a=%d\n",a);
}

(MSVC compiler results in 0.69999... something)


HTH
 
Old 02-17-2004, 04:33 AM   #3
Berng
Member
 
Registered: Dec 2003
Location: Siberia,Russia
Distribution: Basically RedHat
Posts: 34

Rep: Reputation: 15
Looks like it is just default constant type definition.
By default, it is (double) type,
so you get not correct comparison with (float) variable.
The right and accurate variant is to make (float) definition of 0.7 constant before its comparison with float variable, what does the second program
BestRegards,
Oleg.
 
Old 02-17-2004, 04:34 AM   #4
eshwar_ind
Member
 
Registered: Feb 2004
Location: Bangalore
Distribution: Redhat
Posts: 144

Rep: Reputation: 15
When you use 0.7 alone in the programm it is treated as double so
first when float a=0.7;
you are just storing a double precision float value in single precision float location so the value that get stored in a is less that 0.7( as double preceson value). Actually 0.7 is stored as 0.6999999.. some thing like this.
so for first program condition is true.
In second one its false because you are comparing with single precision float value. both are equal here.
 
Old 02-17-2004, 10:15 AM   #5
vinay_s_s
Member
 
Registered: Jul 2003
Posts: 659

Original Poster
Rep: Reputation: 30
can anyone tell me why 0.7 is stored as 0.699999 ???
 
Old 02-17-2004, 12:30 PM   #6
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Because 0.6999999 is the closest you can get to 0.7 in a single precision floating point number.
 
Old 02-17-2004, 12:43 PM   #7
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
http://www.library.cornell.edu/nr/bookcpdf/c1-3.pdf

That PDF gives a better explanation that I could give... basically it comes down to the idea that computers don't have infinite precision. They have a finite amount of space for each part of the floating point number. As a result you cannot represent every number exactly. Because of this you need to be particularly careful when comparing floating point values.
 
Old 02-17-2004, 01:36 PM   #8
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by vinay_s_s
can anyone tell me why 0.7 is stored as 0.699999 ???
This is because computers store numbers in binary using only two digitis: 0 and 1. So a computer has to store a number in powers of two: 0, 1, 2, 4, 8, 16, 32,...

A human usually "store" numbers in decimal using 10 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. We humans store (write down) a number in powers of ten.

For example "zero" point "eight" "one" "two" "five" is written down by humans as:
0.8125. The part after the dot actually means: 8/10 + 1/100 + 2/1000 + 5/10000 (note the powers-of-10 pattern: 10, 100, 1000, 10000)

The same number is stored by a computer (using only 0 and 1 digits) as 0.1101.
The part after the dot means: 1/2 + 1/4 + 0/8 + 1/16 (note the powers-of-2 pattern: 2, 4, 8, 16)

Now the number of this example could be written down (stored) exactly in 5 digits in both decimal and binary form. But try a number very simple for humans like 1/10. We write the value down as "0.1" which is exactly the value 1/10. The very same exact value in binary is: "0.000110011". So humans write 1/10 down in only two digits, computers need ten digits.

Float and double type var's store floating point numbers, but with a fixed number of bytes. In Linux on a x86 a float is 4 bytes and a double is 8. This limits the precision.

Your number 0.7 is 0.10110011 exactly in binary. That would seem to fit in 4 bytes, but a float should also be able to store numbers like 324.7e+11, so much of the four bytes is taken to store the integer part (here 324) and the exponent (here 11). So only part of the 4 bytes are available for storing the fraction (the part after the dot). Your 0.7 didn't fit in, and using powers of two 0.6999 was as close as it could get to 0.7

Last edited by Hko; 02-17-2004 at 01:52 PM.
 
Old 02-17-2004, 02:55 PM   #9
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Rep: Reputation: 38
interesting!
 
Old 02-17-2004, 09:44 PM   #10
vinay_s_s
Member
 
Registered: Jul 2003
Posts: 659

Original Poster
Rep: Reputation: 30
cool thanks all i got what all of u meant.
So the point is that its because of storage limitations and BINARY representaion!!!

Cool thanks a lot.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange C program issue exvor Programming 21 07-06-2005 07:42 PM
Urgent: Call a C program through Linux shell script nuwandee Programming 14 04-10-2004 07:31 PM
URGENT: sendmail setup questions + acting up strange macie Linux - Networking 5 12-29-2003 08:10 PM
Squid and FTP servers - very strange and urgent problem Zingaro2002 Linux - Networking 1 11-10-2003 04:37 AM
need same urgent help with c program black111 Programming 2 05-25-2003 05:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration