Jump to content

How do calculators work with rounding errors?

- - - - -

  • Please log in to reply
9 replies to this topic

#1
invokeLater

invokeLater

    Newbie

  • Members
  • PipPip
  • 11 posts
Hey, I'm just learning C++ and I learned about rounding errors, how do calculators get around this?

#2
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
Calculators have rounding errors as well.
Latinamne loqueris?

#3
invokeLater

invokeLater

    Newbie

  • Members
  • PipPip
  • 11 posts
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
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
Can you show me the example? I'm just curious.
Latinamne loqueris?

#5
invokeLater

invokeLater

    Newbie

  • Members
  • PipPip
  • 11 posts
[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.

#6
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
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
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
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.

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.

#8
invokeLater

invokeLater

    Newbie

  • Members
  • PipPip
  • 11 posts
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.


#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
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
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.

code said:

cout << setprecision(17);
Precision is sometimes irrelevant. If you'd go father than a double's precision you'd get even stranger numbers. i.e.
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.

#10
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
GMP would be a good arbitrary precision arithmetic library.
Latinamne loqueris?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users