473,497 Members | 2,041 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How do you force a computed float value to be stored in a variable?

1 New Member
I have two short programs that use floating point numbers. The first gives an incorrect result. By simply placing a "cout" (or any executable line) in the correct location of the program I get a correct result. The first program is:

Expand|Select|Wrap|Line Numbers
  1. /* floor example */
  2. #include <iostream>
  3. #include <math.h>
  4.  
  5. int main ()
  6. {
  7.     float a=10.0f;
  8.     float b=0.9f;
  9.  
  10.     float c=a*b;
  11.     float d=floor(c);
  12.     std::cout << "floor of " << c << " is " << d << std::endl;
  13.  
  14.     return 0;
  15. }
  16.  
  17.  
which outputs:
floor of 9 is 8

The second program is

Expand|Select|Wrap|Line Numbers
  1. /* floor example */
  2. #include <iostream>
  3. #include <math.h>
  4.  
  5. int main ()
  6. {
  7.     float a=10.0f;
  8.     float b=0.9f;
  9.  
  10.     float c=a*b;
  11.     std::cout << "";  //inserted line of executable code
  12.     float d=floor(c);
  13.     std::cout << "floor of " << c << " is " << d << std::endl;
  14.  
  15.     return 0;
  16. }
  17.  
  18.  
which outputs:
floor of 9 is 9

I am using MSVC++ 6.0 on an XP workstation. (SP2 for XP, and SP6 for VC++) I understand how floating point numbers are represented in the computer. I also know that the representation of c in the first program is 8.999... and in the second program the representation is 9.000. I think that due to compiler optimizations, the value of c in the first program is not being rounded correctly and stored, but the register containg the result of a*b is being used directly without rounding. In the second program the inserted line of executable code seems to cause the value stored in c to be correctly rounded.

Is this a bug in the compiler, and if so is there a fix?
Apr 10 '08 #1
3 3383
Banfa
9,065 Recognized Expert Moderator Expert
Is this a bug in the compiler, and if so is there a fix?
Upgrade to VS 2005 my VS 2005 Express Edition does not seem to exhibit this behaviour the result is always

floor of 9 is 9

This is probably classifiable as a feature rather than a bug. All bets are kind of off if you are going to perform floating point arithmetic.
Apr 10 '08 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
Keep in mind that in C floor() takes a double argument and returns a double. Conversion back to float can cause truncation and possible loss of data. Like maybe what you are seeing.

Do not mix float an double. Unless you have a technical reason that you can write down on paper, use double throughout. float is an obsolete holdover ffrom the days of computers with small memories.
Apr 11 '08 #3
JosAH
11,448 Recognized Expert MVP
What every computer scientist should know about floating-point arithmetic.

kind regards,

Jos
Apr 11 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
7970
by: Bryan R. Meyer | last post by:
I am a relatively new C++ programmer and am attempting to write a function that takes a number of type float and adds commas to it in the appropriate places. In order to manipulate the number to...
5
2151
by: Gianni Mariani | last post by:
The spirit of this arguably pointless exercise, is that the numeric_limits<T> class could be replaced with a totally generic template of compile-time, template computed constants. The problem is...
9
3655
by: DMAC | last post by:
If i want to split a computed column into two or more columns based on the the length (its a varchar) of the computed column, how often will sql server determine what the computed column is?...
8
9546
by: Jonathan Fielder | last post by:
Hi, I have a 32 bit integer value and I wish to find the single precision floating point value that is closest to but less than or equal to the integer. I also have a similar case where I need...
8
3449
by: vijay | last post by:
Hello, What happens to float variable in loops. For example, float f=8.7; if(f<8.7) printf("less"); else if(f==8.7) printf("equal"); else if(f>8.7)
6
7591
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
3
2684
by: bofh1234 | last post by:
I am trying to write a function that returns a float. This is what the function should do: It takes the socket number and a variable name. The function looks up the variable in an array of...
34
2633
by: yaniv.dg | last post by:
hi all, i building an equition with float type now,what function should i use in order to identify is someone enter a char value by mistake?
1
1689
by: Murdz | last post by:
Hello. I have a table with a primary key, identity value. In addition to some other fields I also have a computed field to generate a binary identifier for the row based on the identity value (eg....
0
6993
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
7162
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
7197
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6881
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7375
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...
0
3088
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...
0
1411
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 ...
1
650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
287
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...

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.