Front Office Football Central  

Go Back   Front Office Football Central > Archives > FOFC Archive
Register FAQ Members List Calendar Mark Forums Read Statistics

Reply
 
Thread Tools
Old 02-02-2004, 08:49 PM   #1
Karim
College Starter
 
Join Date: Oct 2000
Location: Calgary
OT: Simple C++ question (newbie)

I was under the impression that float and double were exactly the same thing except for the amount of memory allocated to the variable in question. But I was using double when declaring my variables and discovered that values were being truncated instead of rounded. For the heck of it, I switched over every variable to a float and the rounding occurred correctly.

Has this anything to do with float/double or is this a consequence of the actual mathematical expression I used? I try and not use mixed expression is I can avoid it (plus, I can never seem to remember how the computer will handle it).

Thanks in advance.

Karim is offline   Reply With Quote
Old 02-02-2004, 09:04 PM   #2
Celeval
Pro Starter
 
Join Date: Nov 2000
Location: Cary, NC, USA
It's a float/double thing. IIRC, they use different formulae to hold the value, and therefore there can be certain kinds of rounding errors.
Celeval is offline   Reply With Quote
Old 02-02-2004, 09:33 PM   #3
GoldenEagle
Grizzled Veteran
 
Join Date: Dec 2002
Location: Little Rock, AR
Floating point variables hold values that can be expressed as fractions. I think it would ony be a facotr if you working with very big numbers.
GoldenEagle is offline   Reply With Quote
Old 02-02-2004, 11:21 PM   #4
Mr. Wednesday
Pro Starter
 
Join Date: Jul 2003
Location: South Bend, IN
float/double use different amounts of memory to store the numbers, yes. Things get interesting when you convert between them with values that can't be represented exactly.

Here's the 10 cent binary floating point tutorial -- your computer's floating point format is a binary format. The memory for the number is broken up into a base and an exponent. It's all in base two, which means that the only decimal values that can be represented exactly in binary floating point are negative powers of two e.g. 0.5, 0.25, 0.125, etc. Anything else gets rounded.

Normally, you don't see this rounding, because they go to some effort to hide it from you, but it comes out if you take a number that starts out as a float and turn it into a double.

I'm not really clear on the exact problem you're experiencing; floating point constants in C++ default to double IIRC and unless you're going outside the precision of floats (seven to nine significant figures) with your constants, you shouldn't be seeing anything unusual when you assign to floats.
Mr. Wednesday is offline   Reply With Quote
Old 02-03-2004, 07:47 AM   #5
Fido
High School Varsity
 
Join Date: Aug 2002
Location: New Hampshire, USA
Quote:
Originally Posted by Mr. Wednesday
I'm not really clear on the exact problem you're experiencing; floating point constants in C++ default to double IIRC and unless you're going outside the precision of floats (seven to nine significant figures) with your constants, you shouldn't be seeing anything unusual when you assign to floats.

I agree with this. Can you post the code that you are having problems with? A mathmatical ssignment to a double should be more accurate than an assignment ot a float, and it doesn't make sense that you are seeing trumncation with doubles and not with floats unless there's some other issue that you're not sharing with us.
__________________
Author of FOF Reporter and TCY Helper.
Fido is offline   Reply With Quote
Old 02-03-2004, 10:20 PM   #6
Karim
College Starter
 
Join Date: Oct 2000
Location: Calgary
Ok. Here's the code. Excuse the lack of indenting. Entering a length of 50 and a width of 5, creates a price of $xxxx.83. If I change the variables to a double, I get a price of $xxxx.82.

~~~~~

#include
#include
int main()
{

const float GST = 0.07;
float length, width, area, subtotal, total, cost;

cout << endl;

cout << "Enter the length in feet: ";
cin >> length;

cout << "Enter the width in feet: ";
cin >> width;


area = length * width;

if (area > 500)
{
cost = 11.29;
subtotal = cost * area;
total = subtotal + (subtotal * GST);
cout << "\nThe cost is: $"
<< setiosflags(ios::fixed)
<< setiosflags(ios::showpoint)
<< setprecision(2)
<< total << endl;
}
else if (area < 250)
{
cost = 29.59;
subtotal = cost * area;
total = subtotal + (subtotal * GST);
cout << "\nThe cost is: $"
<< setiosflags(ios::fixed)
<< setiosflags(ios::showpoint)
<< setprecision(2)
<< total << endl;
}

else
{
cost = 20.59;
subtotal = cost * area;
total = subtotal + (subtotal * GST);
cout << "\nThe cost is: $"
<< setiosflags(ios::fixed)
<< setiosflags(ios::showpoint)
<< setprecision(2)
<< total << endl;
}

cout << endl;
return 0;
}

Last edited by Karim : 02-03-2004 at 10:21 PM.
Karim is offline   Reply With Quote
Old 02-03-2004, 10:23 PM   #7
sabotai
General Manager
 
Join Date: Oct 2000
Location: The Satellite of Love
Hmmm....I wonder if the setprecision does somethign different for floats and doubles...don't know why it would though. Only thing "obvious" to me. I'll try to run the code and see if I get the same results.
sabotai is offline   Reply With Quote
Old 02-03-2004, 10:32 PM   #8
sabotai
General Manager
 
Join Date: Oct 2000
Location: The Satellite of Love
I think it's the const. When I change just the variables to double and leave the const as a float, I still get .83, but when I change the const to double it shows .82 so it has to do with that. It doesn't seem to matter how I declare just the variables.
sabotai is offline   Reply With Quote
Old 02-03-2004, 10:40 PM   #9
sabotai
General Manager
 
Join Date: Oct 2000
Location: The Satellite of Love
Ok, here it is.

When the const is declared as a double, after the

total = subtotal + (subtotal * GST);

line, total equals 5507.8250000000

However, when the const is declared as a float, after that line total equals 5507.8250015341

So it would seem that declaring a const as a double rather than a float gives you a more precise calculation.

Last edited by sabotai : 02-03-2004 at 10:43 PM.
sabotai is offline   Reply With Quote
Old 02-03-2004, 11:12 PM   #10
Karim
College Starter
 
Join Date: Oct 2000
Location: Calgary
Great! Thanks, sabotai.
Karim is offline   Reply With Quote
Old 02-04-2004, 12:40 AM   #11
Mr. Wednesday
Pro Starter
 
Join Date: Jul 2003
Location: South Bend, IN
sabotai, yes, that's correct, and expected. The precision for a float is somewhere in the ballpark of eight or nine significant figures, which is exactly what you see there.

Karim, see my abbreviated comments on floating point math. Your numbers are not exactly representable, so on something that breaks either way like this, you cannot rely on a particular result. It's always hazardous to rely on exactness in floating point calculations due to variations in rounding, if you really care about that last decimal digit, you may be better off using something like scaled integers.
Mr. Wednesday is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is On
Forum Jump


All times are GMT -5. The time now is 04:19 AM.



Powered by vBulletin Version 3.6.0
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.