78

Computation in science and engineering requires tradeoffs in precision, range, and speed. Fixed point arithmetic provides precision, and decent speed, but it sacrifices range. BigNum, arbitrary precision libraries, win on range and precision, but lose on speed. The crux of the matter is that most scientific and engineering calculations need high speed, and ...


55

It's called banker's rounding. The idea is to minimize the cumulative error from many rounding operations. Lets say you always rounded .5 down. Think of all those little interest payments, the bank pocketing half a cent each time... Lets say you always rounded .5 up. Accounting is going to scream because you're paying out more interest than you should.


46

Your argumentation against floating point numbers is very fragile, probably because of naivety. (No offense here, I find your question is actually very interesting, I hope my answer will also be.) A classic argument is that floats provide a greater range, but high precision integers can meet this challenge now. For example: with modern 64-bit ...


39

A while ago I constructed a test program for successive rounding, because it's basically a worst-case stress test for a rounding algorithm. For each number from 0 to 9,999 it first rounds to the nearest 10, then to the nearest 100, then to the nearest 1000. (You could also think of this as 10,000 points in [0,1) being rounded to 3 places, then to 2, then to ...


38

There are numerous ways of storing fractional numbers, and each of them has advantages and disadvantages. Floating-point is, by far, the most popular format. It works by encoding a sign, a mantissa, and a signed base-2 exponent into integers, and packing them into a bunch of bits. For example, you could have a 32-bit mantissa of 0.5 (encoded as 0x88888888) ...


35

There are actually modes of numbers that do that. Binary-coded decimal (BCD) arithmetic has the computer work in base 10. The reason you run into this rarely is that it wastes space: each individual digit of a number takes a minimum of four bits, whereas a computer could otherwise store up to 16 values in that space. (It can also be slower, but it's ...


30

What alternative do you propose? Continuous quantities are represented using real numbers in mathematics. There is no data type that can encode every possible real number (because reals are uncountable), so that means we can only pick a subset of those real numbers that we're most interested in. You can pick all computable reals, which is similar to what ...


26

Because switching to integers doesn't solve anything. The problem with floats isn't that they have inaccuracies, it's that half the people using them don't pay any attention to what's going on. Those same people aren't going to pay proper attention to the units they are using when they use an integer, and a different set of screw ups will happen. Repeat ...


25

You are looking for an arbitrary precision arithmetic (also called "multiple precision" or "big num") library for the language you are working with. For instance, if you are working with C you can use the GNU Bignum Library -> http://gmplib.org/ If you want to understand how it works you can also write your own big num library and use it. The simplest way ...


21

Physical characteristics of the universe (like the number of atoms in it) are not useful to determine the boundaries of number sizes, because useful calculations exist using numbers having wider ranges. Floating point numbers are a tradeoff between accuracy and range. They deliberately give up some accuracy to achieve greater range.


15

Imagine for a moment that the quantity you are interested in has a value in the range 42.0 - 42.999. Imagine further that you want as much precision as possible. As it stands, you are spending a chunk of your available bits representing the value 42, and that leaves fewer bits available to represent the 0.000 - 0.999, which in some sense is what you are ...


14

Your proposition about science is wrong, Engineering and Science other then Math don't work with exact precise results. They work with a precision factor which is built into how many digits you show. The key term you need to understand here is: significant figures. The significant figures of a number are those digits that carry meaning contributing to its ...


13

It is a well known problem: Arbitrary precision arithmetic When the language you are using doesn't resolve this problem, first try to find a third party library that does. If you don't find it or you are curious, try to implement it; the wikipedia article has a good references to classical implementations.


13

Because most of the processors that you use in your day to day life are not modern day 64 bit processors with crazy fast integer calculations or an over abundance of space. Most of your processors are 8-16 bit devices which run things like your car, microwave, or watch. Besides, what happens when you need to talk about a half of a unit, like a half of a ...


12

Monetary values in general do not use floating point (always approximation errors!), but fixed point, as BigDecimal in java, DECIMAL in SQL. For currencies you then have a defined precision (2 decimals for instance). Now national regulations prescribe the precision of some calculations, like taxes with a precision of 6 (Europe). These kind of precisions ...


10

If floating point values have rounding problems, and you don't want to have to run into rounding problems, it logically follows that the only course of action is to not use floating point values. Now the question becomes, "how do I do math involving non-integer values without floating point variables?" The answer is with arbitrary-precision data types. ...


10

If the documentation makes no special mention, is it implied that these kinds of functions are completely accurate to the last decimal place, within the precision offered by IEEE double-precision floating-point? I wouldn't make that assumption. Where I work we deal with telemetry data, and it's common knowledge that two different math libraries can ...


9

Consider this hypothetical. You have two data types. One is a floating point number with 32 bits of precision that is base 3 (represented by the digits 0, 1, and 2). The other is a floating point number with 64 bits of precision that is base 4. Given this code: float a = .3 + .6 float b = .9; print(a == b) The comparison will return true if base 3 ...


9

There are two levels of rounding you should be doing. The primary one is in the business logic to round to the level of precision required. The UI can round for display purposes independently. Rules around this should be part of your requirements, some of them are also likely derived from laws that may specify precision of various transactions or how to ...


7

Floating point arithmetic is usually quite precise (15 decimal digits for a double) and quite flexible. The problems crop up when you are doing math that significantly reduces the amount of digits of precision. Here are some examples: Cancelation on subtraction: 1234567890.12345 - 1234567890.12300, the result 0.0045 has only two decimal digits of precision. ...


7

Note that floating point numbers are basically the same as scientific and engineering notation, the standard way for humans to write numbers in math and science. In these fields, there's not a great need for extreme precision, but there's often a huge range. To pick a random example from my physics homework, I recently had to work with the mass of an ...


6

When dealing with large numbers, probably one of the most fundamental design decisions is how am I going to represent the large number? Will it be a string, an array, a list, or custom (homegrown) storage class. After that decision is made, the actual math operations can be broken down in smaller parts and then executed with native language types such as ...


6

Floating point numbers represent a vast range of values, which is very useful when your don't know ahead of time what the values might be, but it's a compromise. Representing 1/10^100 with a second integer wouldn't work. Some languages (and some libraries) have other characteristics. Lisp traditionally has infinite precision integers. Cobol has ...


6

The study of stability of floating point computation is part of numerical analysis and if you really want a sound result, you really want someone knowledgeable in that domain to do the analysis of the algorithms used. There are some things which can help to experimentally identify unstable algorithms. Running with rounding set to different modes (up/down/...


6

Floating-point numbers also have several properties that lend themselves well to computing certain types of scientific results. Most notably, precision is inversely proportional to magnitude, just like in scientific notation, so you can represent both small differences close to zero and larger differences much farther away. Goldberg's paper is probably the ...


Only top voted, non community-wiki answers of a minimum length are eligible