MPH couts 1.#INF -- not sure why... even if I use setprecision. Any ideas?Code:{ float MPH, hours; int Miles; Miles = 37 + ((15000/5280) + (12000/YARDS_IN_MILES)); hours = 45/60; MPH = Miles/hours; cout << "MPH: " << MPH; return MPH; }
This is a discussion on float value returning: 1.#INF within the C++ Programming forums, part of the General Programming Boards category; Code: { float MPH, hours; int Miles; Miles = 37 + ((15000/5280) + (12000/YARDS_IN_MILES)); hours = 45/60; MPH = Miles/hours; ...
MPH couts 1.#INF -- not sure why... even if I use setprecision. Any ideas?Code:{ float MPH, hours; int Miles; Miles = 37 + ((15000/5280) + (12000/YARDS_IN_MILES)); hours = 45/60; MPH = Miles/hours; cout << "MPH: " << MPH; return MPH; }
Well: hours is set to 0.0 (since 45/60 is 0 remainder 45, and remainder don't count); and dividing by 0.0 gives 1.#INF, since the fancy infinity sign is hard to print.
In other words: do hours = 45.0/60.0; instead.
The result of this expression - dividing an integer 45 by an integer 60 yields zero. Then, you divide Miles by zero.
Todd
Mac and Windows cross platform programmer. Ruby lover.
Quote of the Day
12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.
Amen brother!
And as a metric user, I have no idea what YARDS_IN_MILES is, but if it's a integer, too, there's another bad calculation.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
This thread has been dormant for over 3 months. If the OP still had the problem, he'd have posted.
Please don't revive old threads.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law