473,386 Members | 1,828 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,386 software developers and data experts.

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

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 3378
Banfa
9,065 Expert Mod 8TB
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 Expert Mod 8TB
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

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

Similar topics

5
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.