Front Office Football Central

Front Office Football Central (https://forums.operationsports.com/fofc//index.php)
-   FOFC Archive (https://forums.operationsports.com/fofc//forumdisplay.php?f=27)
-   -   OT: Simple C++ question (newbie) (https://forums.operationsports.com/fofc//showthread.php?t=21097)

Karim 02-02-2004 08:49 PM

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.

Celeval 02-02-2004 09:04 PM

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.

GoldenEagle 02-02-2004 09:33 PM

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.

Mr. Wednesday 02-02-2004 11:21 PM

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.

Fido 02-03-2004 07:47 AM

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.

Karim 02-03-2004 10:20 PM

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;
}

sabotai 02-03-2004 10:23 PM

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 02-03-2004 10:32 PM

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 02-03-2004 10:40 PM

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.

Karim 02-03-2004 11:12 PM

Great! Thanks, sabotai.

Mr. Wednesday 02-04-2004 12:40 AM

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.


All times are GMT -5. The time now is 08:58 AM.

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