Exforsys

Online Training

Problem with float

This is a discussion on Problem with float within the C and C++ forums, part of the Programming Talk category; When I compiled and ran my C program given below: #include <stdio.h> main() { float c=0.9 ...


Go Back   Exforsys > Programming Talk > C and C++

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 06-05-2007, 03:57 AM
Senior Member
 
Join Date: Apr 2006
Posts: 145
Rahulbatra is on a distinguished road
Problem with float

When I compiled and ran my C program given below:
#include <stdio.h>
main()
{
float c=0.9
if(c<0.9)
printf("Welcome");
else
printf("Bye");
}

The above simple program output puzzled me. I was sure that output would be Bye since value of c equal to 0.9 is not less than 0.9. But I got output as Welcome. Why is this? Kindly help me to know about this.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 06-06-2007, 04:35 AM
Junior Member
 
Join Date: May 2007
Posts: 12
Mukhtar Ahmad is on a distinguished road
you must postfix "f" when assingn constants float values like

float c=0.9f;
and rest of statements
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-06-2007, 07:47 AM
Senior Member
 
Join Date: Apr 2006
Posts: 139
Ralph is on a distinguished road
The output was welcome because of the reason of precision consideration. You have declared variable c as float and stored value 0.9 in that. But inside the memory it would get stored as a value with a little less value than 0.9 due to precision consideration and so when you compare c with 0.9 as c<0.9 it returns true and prints welcome. If you want to print Bye in the above program you can declare variable c as long double as
long double c;
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 06-11-2007, 10:59 AM
Junior Member
 
Join Date: Jun 2007
Posts: 5
srknth_chin2 is on a distinguished road
hey

do u check the result for 1.9,2.9,3.9,4.9,...
for 0.9 n 1.9 the o/p is welcome
for the remaining ....2.9,3.9....the o/p will b bye
check it
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 07-02-2007, 06:09 AM
Junior Member
 
Join Date: Jul 2007
Posts: 3
Bairaviuthra is on a distinguished road
Reg Float point comparison

<p>Hello Rahul..<br>
Here is an explanation to your doubt..<br>
in your example you have specified the value of c as 0.9<br>
as we know well floating point variables can hold upto 4 bytes in length.<br>
so the actual value of c will be assigned by the compiler as 0.90000<br>
take a note of the trailing 4 characters after 9.<br>
so, when you try to compare floating point variables / numbers, compare it with<br>
its full precision of completed float (double) format to get your expected
results..<br>
hope you understood.. in case of any suggestions to my above explanation, do <br>
drop a mail to bairavi DOT gita AT gmail DOT com<br>
Regards,<br>
Bairavi</p>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 09-10-2007, 03:37 AM
Junior Member
 
Join Date: Sep 2007
Posts: 1
Shyamapada is on a distinguished road
0.89 < 0.90

The ans is as follws

if we convert 5.0 to its binary foramat to sore it in a 4 byte float memory then 5 is equivalent to 101 and it will be stored at mantissa and corerespondingly exponenet will contain 1 as

5.0 = 0.5 x E^1 This is normalized form of floating numbers.

But another form is direct store of integer part as well as fractional part. And this procedure has been explained below to ans your question. Go through it.

Take the following example------------>
5.75

5 % 2 =1
2% 2 = 0
1
So 5 --> 101

and...........
0.75 x 2 = 1.50 ---> 1
0.50 x 2 = 1.00 ---> 1
So 0.75 --> 0.11

So ans 5.75 is 0000000000000101.1100000000000000 in 4 byte float memory

Now consider the case 0.9

0.90 x 2 = 1.80 ---> 1

0.80 x 2 = 1.60 ---> 1
0.60 x 2 = 1.20 ---> 1
0.20 x 2 = 0.40 ---> 0
0.40 x 2 = 0.80 ---> 0

0.80 x 2 = 1.60 ---> 1
0.60 x 2 = 1.20 ---> 1
0.20 x 2 = 0.40 ---> 0
0.40 x 2 = 0.80 ---> 0

0.80 x 2 = 1.60 ---> 1
0.60 x 2 = 1.20 ---> 1
0.20 x 2 = 0.40 ---> 0
0.40 x 2 = 0.80 ---> 0

So this will continue upto infinity

So your actual ans is
A=0000000000000000.1110011001100110 01100110011001100.........
But stored value in memory is
C=0000000000000000.1110011001100110 00000000000000000.........

Thats why C<A.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 09-17-2007, 10:06 AM
Junior Member
 
Join Date: May 2007
Posts: 1
sunit is on a distinguished road
you have written code like c<0.9 in if condition. Insted of write c(float)<0.9 you will get correct output.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 09-17-2007, 03:20 PM
crazyamar
 
Join Date: Sep 2007
Location: guntur
Posts: 1
amarwfs is on a distinguished road
Send a message via Yahoo to amarwfs
Thumbs down ooooooooooo

Quote:
Originally Posted by Rahulbatra View Post
When I compiled and ran my C program given below:
#include <stdio.h>
main()
{
float c=0.9
if(c<0.9)
printf("Welcome");
else
printf("Bye");
}

The above simple program output puzzled me. I was sure that output would be Bye since value of c equal to 0.9 is not less than 0.9. But I got output as Welcome. Why is this? Kindly help me to know about this.
hai ,]
this is amar i am also facing the same problem
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 09-19-2007, 11:33 PM
Junior Member
 
Join Date: Nov 2006
Posts: 1
neopravdano_odsutan is on a distinguished road
It all depends which programming tool do you use. For example, I tried this problem on Dev-C++ 4.9.9.2 and found similar solution as Mr. sunit, only a bit different (may be he used another IDE),and it goes like this:

if (c<(float)0.9)
.
.
.

Because variable c is FLOAT,you must compare it with FLOAT.And yes,we all know from math that 0.9 is FLOAT,but you must explicitly tell compiler that 0.9 is FLOAT,and you do that putting (float) in front of 0.9.
I think this is called OVERRIDING,but I am not sure,because my english is very bad.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 10-11-2007, 12:01 PM
Junior Member
 
Join Date: Oct 2007
Posts: 1
aryan1431 is on a distinguished road
Thumbs up

Quote:
Originally Posted by Rahulbatra View Post
When I compiled and ran my C program given below:
#include <stdio.h>
main()
{
float c=0.9
if(c<0.9)
printf("Welcome");
else
printf("Bye");
}

The above simple program output puzzled me. I was sure that output would be Bye since value of c equal to 0.9 is not less than 0.9. But I got output as Welcome. Why is this? Kindly help me to know about this.
The answer for ur problem is : In general float precision value is '6'.
that means when u enter a float value of 1.2or 0.9 it will reads it as 0.900000.which is different from 0.9.that is why it is showing wrong out put. if u want to give the exact float value take itas "0.9f"
u will get the right result.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

Thread Thread Starter Forum Replies Last Post
Artificial Intelligence FAQ:1/6 General Questions & Answers [Monthly posting] Ric Crabbe and Amit Dubey Tech FAQ 0 06-02-2004 01:12 AM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit Tech FAQ 0 06-01-2004 07:00 AM
Artificial Intelligence FAQ:1/6 General Questions & Answers [Monthly posting] Ric Crabbe and Amit Dubey Tech FAQ 0 05-05-2004 04:13 PM
PSION Series 3/3a palmtop FAQ part 3/6 Daniel `HB9VBC' Pfund Tech FAQ 0 04-17-2004 08:27 AM
Solving Lazy Initialization and double checked locking problem Vinay Aggarwal Software Patterns 19 02-18-2004 05:34 PM


All times are GMT -4. The time now is 06:15 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2004 - 2007 Exforsys Inc. All rights reserved.