3 ms·
For the tax one, what rounding rules apply? How many significant digits are in the dollar amount and tax rate? Do I have access to IEEE 854 decimal or should I
by dalke 11y ago
For the tax one, what rounding rules apply? How many significant digits are in the dollar amount and tax rate? Do I have access to IEEE 854 decimal or should I used scaled integers? And to how many digits?
This is important! Consider the following Python code, which shows how the order of operations can change the result by a penny:
>>> d = 6405 # amount in dollars
>>> r = 0.018 # tax rate of 1.8%
>>> int(d * r * 100)
11529
>>> int(r * 100 * d)
11528
This occurs because:
>>> d * r * 100
11529.0
>>> r * 100 * d
11528.999999999998
whereas if I use 854 math:
>>> from decimal import Decimal as D
>>> d = D("6405")
>>> r = D("0.018")
>>> int(d * r * 100)
11529
>>> int(r * 100 * d)
11529
The person who proposed the question believed that the key point was for the interviewee to ask why the result was to be returned in an array. As an inverted fizz-buzz test, this question really reveals that the questioner has little experience with the details of doing accurate math on a computer.
- raverbashing 11y agoYou're absolutely right in your worries, but I think they usually don't consider those factors in the interview (bonus points if you mention it usually) I usually prefer to work using integer cents, Decimal has some quirks in Python (like a unique precision for all numbers)
- dalke 11y agoWell, that is why I called it an 'inverted fizz-buzz test'. The test itself reveals limitations in the organization doing the interviewing. While you may be able to work in integer cents, there is still the question of how you handle rounding. Does Richard Pryor get your fractional cent, a la the salami slicing in Superman 3? Banks typically work in five or six decimal places, and not the 2 decimals you can use. As a specific example, the final conversion rates from the national currencies to the Euro are given to 6 significant digits, eg, 1.95583 Deutsch marks to the Euro. Zimbabwe had problems when their hyperinflation caused overflows in systems that assumed 64 bit integers was enough for any currency. I don't understand what you mean by "quirks in Python". Your concern seems to be with the General Decimal Arithmetic Specification, and not its implementation in Python.
- raverbashing 11y agoSure, rounding is important as well. > I don't understand what you mean by "quirks in Python" Setting a precision for decimal numbers "globally" in the module. https://docs.python.org/2/library/decimal.html https://docs.python.org/2/library/decimal.html But of course, working with cents might get complicated if you're doing a lot of calculations, people should use Decimal Then there was Django's Decimal field which wouldn't even do comparisons correctly and was filled with bugs (in 1.2 at least)
- dalke 11y agoMinor correction; the decimal context is thread-local, and not set '"globally" in the module'. It's similar in philosophy to C99's support for IEEE 754's rounding modes and exception modes. Compare to http://en.cppreference.com/w/c/numeric/fenv http://en.cppreference.com/w/c/numeric/fenv : > The floating-point environment is the set of floating-point status flags and control modes supported by the implementation. It is thread-local, each thread inherits the initial state of its floating-point environment from the parent thread. Since standard floats in C have similar behavior, it's hard for me to agree that it's really a "quirk".