Hey, I'm just learning C++ and I learned about rounding errors, how do calculators get around this?
9 replies to this topic
#1
Posted 23 December 2011 - 09:30 PM
|
|
|
#2
Posted 23 December 2011 - 09:32 PM
#3
Posted 23 December 2011 - 09:34 PM
I was playing with the same examples I used in the compiler and the calculator gave the correct answer. Are they somehow limited in a calculator?
#4
Posted 23 December 2011 - 09:35 PM
#5
Posted 23 December 2011 - 09:44 PM
[ATTACH=CONFIG]4428[/ATTACH]
I just thought of a possibility.. is the precision set based on the number you are working with in a calculator? That would pretty much handle this kind of error.
I just thought of a possibility.. is the precision set based on the number you are working with in a calculator? That would pretty much handle this kind of error.
#6
Posted 23 December 2011 - 10:30 PM
I couldn't see your attachment. Also, I don't think so, but it probably depends on the calculator. What kind of calculator were you using?
Latinamne loqueris?
#7
Posted 24 December 2011 - 12:35 AM
Ponder thou use of ye standard calculator, it'll likely use binary coded decimal system to work in decimals (0-9) rather than fractions (1/128 + ..., base2) at cost of 4 bits per decimal.
Scheme:
Precision advantage:
0.123 = 0000 0001 0010 0011 (0 1 2 3) in BCD
0.123 = 0.00011111011111001110110110010001011010000111001010110000001000 = 0.12299999999999999995836663657655662973411381244659423828125 = requires rounding due to base2, will fail eventually when operating on it.
excerpt from linked article said:
Various BCD implementations exist that employ other representations for numbers. Programmable calculators manufactured by Texas Instruments, Hewlett-Packard, and others typically employ a floating-point BCD format, typically with two or three digits for the (decimal) exponent. The extra bits of the sign digit may be used to indicate special numeric values, such as infinity, underflow/overflow, and error (a blinking display).
Scheme:
[I]1 2 3 4 5 6 7 − (neg)[/I] [COLOR=#000000][FONT=monospace]0001 0010 0011 0100 0101 0110 0111 1101 i.e. 12 = 0001 0010 not 1100 [/FONT][/COLOR]
Precision advantage:
0.123 = 0000 0001 0010 0011 (0 1 2 3) in BCD
0.123 = 0.00011111011111001110110110010001011010000111001010110000001000 = 0.12299999999999999995836663657655662973411381244659423828125 = requires rounding due to base2, will fail eventually when operating on it.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#8
Posted 24 December 2011 - 01:49 PM
Ah, I understand what you're saying Alexander :) .. thanks .. btw i used the windows calculator and my scientific calculator.
In my example, I added 0.1 ten times and had the precision for cout set to 17. The answer the compiler gave was 0.9999999999999999 .
Heres my code, you can compile it and see what's your result.
In my example, I added 0.1 ten times and had the precision for cout set to 17. The answer the compiler gave was 0.9999999999999999 .
Heres my code, you can compile it and see what's your result.
#include <iostream>
#include <iomanip>
int main()
{
using namespace std;
cout << setprecision(17);
double dValue;
dValue = 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1;
cout << dValue << endl;
}
#9
Posted 24 December 2011 - 02:20 PM
0.1 in base 2 is a little more involved. You are performing "0.0999999999999999999 times 10" as 0.1 cannot be represented in binary.
Precision is sometimes irrelevant. If you'd go father than a double's precision you'd get even stranger numbers. i.e.
To be more general, calculators work on "0 point 1" separately with simple algorithms rather than a binary representation of 0.1 and is how they are generally more correct. Binary floating point numerics are not to be really relied on unless they need to be fast and plenty (i.e. rough simulations or games)
You may wish to look in to arbitrary precision algorithms to work around this.
code said:
cout << setprecision(17);
0.0999999999999999999132638262011596452794037759304046630859375
To be more general, calculators work on "0 point 1" separately with simple algorithms rather than a binary representation of 0.1 and is how they are generally more correct. Binary floating point numerics are not to be really relied on unless they need to be fast and plenty (i.e. rough simulations or games)
You may wish to look in to arbitrary precision algorithms to work around this.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#10
Posted 24 December 2011 - 04:50 PM
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









