Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How do calculators work with rounding errors?

calculator

  • Please log in to reply
9 replies to this topic

#1 invokeLater

invokeLater

    CC Newcomer

  • Member
  • PipPip
  • 11 posts

Posted 23 December 2011 - 09:30 PM

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

#2 mebob

mebob

    CC Devotee

  • Validating
  • PipPipPipPipPipPip
  • 467 posts
  • Programming Language:C, C++, Assembly
  • Learning:PHP

Posted 23 December 2011 - 09:32 PM

Calculators have rounding errors as well.
  • 0
Latinamne loqueris?

#3 invokeLater

invokeLater

    CC Newcomer

  • Member
  • PipPip
  • 11 posts

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?
  • 0

#4 mebob

mebob

    CC Devotee

  • Validating
  • PipPipPipPipPipPip
  • 467 posts
  • Programming Language:C, C++, Assembly
  • Learning:PHP

Posted 23 December 2011 - 09:35 PM

Can you show me the example? I'm just curious.
  • 0
Latinamne loqueris?

#5 invokeLater

invokeLater

    CC Newcomer

  • Member
  • PipPip
  • 11 posts

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.
  • 0

#6 mebob

mebob

    CC Devotee

  • Validating
  • PipPipPipPipPipPip
  • 467 posts
  • Programming Language:C, C++, Assembly
  • Learning:PHP

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?
  • 0
Latinamne loqueris?

#7 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

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.

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.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#8 invokeLater

invokeLater

    CC Newcomer

  • Member
  • PipPip
  • 11 posts

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.

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

  • 0

#9 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

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.

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.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#10 mebob

mebob

    CC Devotee

  • Validating
  • PipPipPipPipPipPip
  • 467 posts
  • Programming Language:C, C++, Assembly
  • Learning:PHP

Posted 24 December 2011 - 04:50 PM

GMP would be a good arbitrary precision arithmetic library.
  • 0
Latinamne loqueris?





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download