r/AskProgrammers • • 8d ago

Why Do Floating-Point Errors Happen, and How Do Rockets Handle Numerical Precision?

I’ve always been curious about why seemingly simple decimal calculations can produce unexpected results.
For example:
• Why does 0.1 + 0.2 result in 0.30000000000000004 in many programming languages?
• Why can’t computers represent certain decimal values precisely?
• How does floating-point representation actually work at the hardware level?
More importantly, how do systems where precision is critical deal with this?
For example, spacecraft navigation, rocket guidance systems, orbital calculations, and scientific simulations require incredibly accurate computations.
Do they use higher-precision floating-point formats (64-bit, 128-bit), arbitrary-precision arithmetic, fixed-point arithmetic, or entirely different techniques?
I’d love to understand how engineers manage numerical precision in real-world, high-stakes systems.

11 Upvotes

54 comments sorted by

11

u/pixel293 8d ago edited 8d ago

This talks about the why: https://0.30000000000000004.com/

As to how they deal with, the answer as always "it depends." Sometimes it's using higher precision and rounding the answer or fix-point arithmetic. If calculation speed is not a factor then using an arbitrary-precision library is the way to go. It can also be just doing add/subtracts/multiplication/division in a very specific order to reduce how much error creeps into the calculations.

I should also mention that the military Patriot system had an issues where it kept adding 0.1 to a total, and eventually the number would be off by enough that missiles would miss their target. Their solution was to reboot the system after so many hours of operation. Which is another way to deal with the problem. :-)

5

u/RainbowCrane 8d ago

I was in college for computer science when the Patriot Missile bug was discovered during the first Gulf War and it came up as a topic in nearly every class that year. It’s an excellent demonstration of how roundoff error can become a critical factor in real world applications.

It’s also a great example of how naive code reuse can bite you in the ass. The problem was caused because the same clock code had been copied from missile platform to missile platform for years because it just worked. But the Patriot Missile had 2 separate routines converting that clock value to floating point, the old legacy code and a newer, more accurate chunk of code that was introduced to account for the target missile moving at high speed (as opposed to being a stationary or slow moving ground target). Those two conversions were slightly different and, as you said, over time the error accumulated.

It’s a humbling lesson to programmers about how a seemingly arcane topic like floating point arithmetic can result in real people getting dead.

2

u/Ythio 8d ago

It's only true for IEEE754 implementation, and decent programmers would not use such types for any critical calculation.

6

u/stevevdvkpe 8d ago

IEEE754 is no worse than other floating-point formats for critical calculations, because they all have finite precision. Getting correct results for critical calculations is much more about arranging calculations to minimize numerical error and understanding error propagation and handling of numerical uncertainty (because in real applications the numbers you're working with always come with their own measurement uncertainties). If your numerical representation has lower uncertainty than the numbers you're working with then it's adequate to the task.

2

u/marc_b_reynolds 7d ago

The "Patriot Missile" problem wasn't due using binary 754. It's a much longer chain of "problems" (hardware and human). Spitball: change of "design space", a 1970 24-bit integer processor updating counter by one every 1/10 of sec...which then went to FP which can't exactly represent multiples of 1/10 and compounding of "errors" due to this. There's many write-up FWIW...here's one: https://publikationen.bibliothek.kit.edu/1000181916/160370039

2

u/ih-shah-may-ehl 6d ago

No it's true for any general purpose floating point implementation.

And this is also why we shouldn't call programmers software engineers if they didn't get an actual engineering degree, because actual engineers understand the math behind measurement and notation accuracy and learn to calculate things like accuracy and tolerance to make sure they don't get bitten in the ass.

1

u/everyonemr 8d ago

Resetting to a ground state is a perfectly acceptable way to handle floating point drift. Hard reboots are not an acceptable way to reset the state of a missile defense system.

1

u/tonsofmiso 7d ago

Man I wish I knew any decent programmers

1

u/Resistor1 6d ago

What would an AI choose to use I wonder....?

1

u/ahferroin7 7d ago

One of the other big ones that can impact things is differences in rounding rules. Some algorithms are inherently dependent on how rounding works, and will produce different results if the rounding rules change.

And not all FP implementations support all rounding modes, and some only support one specific mode (though those are thankfully increasingly rare), and it’s not unusual for the default rounding mode to differ between implementations. So FP code that needs to be portable needs to either be tested against all the rounding modes across a wide dataset to confirm that the algorithm is stable (which could sometimes require testing on multiple systems historically, or paying for the less common but often more expensive hardware that supported every standard rounding mode), or needs to enforce usage of a specific rounding mode at runtime.

1

u/Levix1221 2d ago

I'll never forget a rounding function I found in some old flow computer code. It was called round_to_even() and it would look to the next significant digit if it encountered a 5.

1.6 --> 2
1.51 --> 1 (skip the 5, look at the 1)
1.56 --> 2 (skip the 5 look at the 6)
1.550 --> 1
1.556 --> 2

To this day I have absolutely no idea why that code existed, or what 1.5 rounded to with their logic.

Needless to say nobody touched that code when we were porting the code.

6

u/sagetraveler 8d ago

For the same reason you can't represent 1/3 exactly in base 10 decimal notation, you can't represent most fractions exactly in base2 fractional notation. Whenever you divide two numbers, you get an endless string of repeating digits.

2

u/johndcochran 7d ago

Hell, you can't represent most fractions in base 10 either. Nor can you in base 3. Base 5. Base 7. Or for that matter any base at all. As long as you're dividing by any number that has a prime factor that's not in your base, you can't represent it exactly. And since base 10 happens to have an extra prime factor over base 2, it can handle fractions that involve only those divisors using only 2 and 5 as their prime factors.

3

u/Siebje 8d ago edited 8d ago

The are infinitely many numbers between 0 and 1, so you will never be able to represent every single one of them with a finite precision number. For examples of how this is solved, look up IEEE 754, which defines the standard for floating point numbers.

As for 0.3: think of binary representation of a series of fractions with powers of 2. You cannot exactly represent 0.3 as a combination of such fractions. Similar to how you cannot write 1/3 in base 10, it results in an infinitely repeating pattern: 0.0100110011001... which is 1/4 + 1/32 + 1/64 + 1/512 + 1/1024... Which for 32 but float comes to 0.300000011.

Most (if not all) languages have arbitrary precision libraries (see bignum) that allow for more precision than the machines primitives allow, at the cost of speed. Based on the application, you could also choose to use fixed point representation, since for many applications, only a specific range will ever be used (think about possibility computation: the outcome will always be a value between 0 and 1, so there's little value in using a format that is able to store values outside that scope).

3

u/Unable-Primary1954 8d ago edited 7d ago

Computers use IEEE754 norm, so they use base 2 instead of base 10. Hence the difficulties to represent 0.1. If you want to know the details, look at IEEE754 on Wikipedia.

Double precision floating number (64 bit) is the norm (except in GPU) and is enough for most applications (This is equivalent to ~15 decimal digits for significand. You could encode Sun-Earth distance with a below millimeter precision).

For some applications, quad-precision might be used, but that is cumbersome. But being slightly careful when coding is usually enough.

In critical applications, you might use formal methods to prove that your code does not make too big errors, or use interval arithmetic to have precise bounds on the results.

1

u/One-Program6244 7d ago

I actually came across a live bug in our code which exhibited this error. In our case the error resulted in a price that was occasionally one penny out from the correct answer so we may have charged a penny more or less than their shown quote in some cases.

1

u/Unable-Primary1954 7d ago

Yes, base 2 is definitely going to be an annoyance in financial applications. The obvious fix is to count pennies rather than pounds/dollars, but that's not what people are going to do by default.

1

u/nicuramar 1d ago

For financial, it’s often better to use a decimal based type.

3

u/SmokeyKatzinski 2d ago

tbf 0.1 isn't 0.1 and 0.2 isn't 0.2 to begin with...

2

u/Ok_Carrot_896 8d ago

There's a very good talk on CppCon 2015 around this exact topic, but it's a bit lengthy (38 minutes). I recommend watching it nonetheless.

1

u/tcpukl 8d ago

How is that lengthy for a conference?

4

u/aruisdante 8d ago

Lengthy for a conference? It’s not. Lengthy for the attention span of the average redditor? Absolutely. 

1

u/Ok_Carrot_896 8d ago

Not lengthy for a conference, just lengthy of a watch on youtube in general for a lot of people. There's probably videox explaining floating point in under 10 minutes.

2

u/kabekew 8d ago

I don't know about rockets, but for a satellite project I worked on we used double precision (64 bit) which was suitable enough for the ECEF coordinate system we used. Then I remember we had a bunch of macros and overloaded operators that compensated for things like underflow, checked for values like NaN and used a small epsilon for direct floating point comparisons (e.g. (fabs(val1 - val2) <= FP_EPSILON) instead of "val1 == val2").

If there was a hardware exception because of a floating point error it indicated a bug, because it normally wasn't supposed to happen because of the above (and there was a whole exception recovery and watchdog system if it ever did happen in actual use).

How floating point is implemented in hardware is something you can google, that's too lengthy for a post here.

2

u/y-c-c 6d ago edited 6d ago

The 0.1+0.2 example is not an “error”. It just comes from the fact that computers tend to use base 2 fractional numbers. There is no 1 to 1 mapping between that and base 10 so you will get some numbers that you cannot represent precisely in another system. If you need base 10 numbers (say for finance) there are lots of ways to do that in software (either by using fixed point arithmetic or manually implementing base 10 floating point) but they are not as efficient as base 2 floating points on most hardware.

Another point to note is that most modern programming languages do in fact print 0.1+0.2=0.3. This is because when you display a base 2 fractional number in base 10 you can choose to round up or down. Modern algorithms will choose it smartly to show a nicer number which in this case is “0.3”. The default printf in C doesn’t do that because it’s an old function that has well-defined behaviors. Look for an old function called dtoa by Dave Gray to see how this is a somewhat non-trivial problem to do it efficiently.

A problem that sometimes get interpreted as “errors” is that order of calculations matter. So (a+b)*c may have a slight difference from ac+bc. This is due to how rounding works. Given that compilers can have leeway in how they generate the machine output you do have to be careful you a completely deterministic output from floating point calculations across different hardware and compilers.

Most spacecrafts just use normal floating points (say, 64 bits). It pretty much has the precision you need, and floating points allow you to slide the scale to the magnitude you need. Usually the thing you need to pay attention comes from making sure you set the origin well (if the number goes too big they lose precisions, and it’s best to use numbers between 0 and 1), and to make sure you avoid adding a very large floating point and a very small one. These are tricks you need to use in other fields like video games. This js actually a very deep topic as it really depends on your needs and you may need to analyze the range of numbers and how stable you need them to be. However, it is important to point out this issue is related to numerical stability, not random floating point “errors”.

One thing spacecrafts do have to worry about is random bit flips due to space radiation (this is the only part that has actual errors). In addition to physical shielding, one approach (among others) is to run the same calculation on multiple computers and let them vote. For example say for a Falcon 9 rocket, all the important calculations are done in three separate computers that vote on the result. If one is wrong for whatever reason the other two will reset it back to a synchronized state. This isn’t specific to floating point though. You would need to deal with this even with integers.

2

u/ExtraTNT 8d ago

What is 1/pi? 1/pi = 0.1 in base pi… in base 10, it’s a mess and you need to round… hope, this explains it…

1

u/johlae 8d ago

The Forth computer language figured it out long ago: https://www.forth.com/starting-forth/5-fixed-point-arithmetic/

1

u/Leverkaas2516 8d ago edited 8d ago

More importantly, how do systems where precision is critical deal with this?

The usual technique is to write code that compares floating point quantities as approximations.

That is, you don't write "if (X == Y)", you write "if abs(X - Y) < E" where E is defined to be a small but reasonable number. If you know the values of X and Y are supposed to be approximately equal but they might differ in the 18th decimal place because of round-off errors, but any difference of 0.001 or more is a significant difference, then maybe choose E to be 0.0001.

The other main idea is that some algorithms are much more sensitive to accumulated rounding errors, so you don't use them. If you can use an algorithm that works with integers, you prefer to do so (they're often faster) as long as it doesn't needlessly obfuscate the code.

1

u/lkruijsw 8d ago

It is all binary. 1/3 in decimal system is a repetitive fraction: 0.33333333... The reason 3 is not a factor of 10.

In binary 1/5 (0.2) is a repetitive fraction in binary: 0.001100110011...

Because 5 is not a factor of 2.

So, is 1/10 (0.1)

ABC the predecessor of Python had true fractions.

1

u/jewishSpaceMedbeds 8d ago

This is a whole field of programming (numerical analysis).

In short, these errors are due to the way numbers are represented in computers. You cannot reliably represent a fractional number with a finite sequence of bits, a bit like you can't represent some fractional numbers (periodical and irrational) with finite decimal notation. So you have to truncate. Truncations are errors that tend to accumulate with the number of operations you are doing, and there are ways to order operations to minimize it.

These are especially important when you calculate things in multiple steps with approximations because the processor cannot directly perform the operation directly (ex.: exponentials, logarithms, square roots, etc.).

1

u/Ythio 8d ago

Because you have the impossible task of representing an infinite amount of numbers in a finite amount of memory. And do it in a way that makes calculations quick.

1

u/Ill_Huckleberry_2079 8d ago

> How does floating-point representation actually work at the hardware level?
Are you sure you want the answer to this question? Because if you do here it is, it's 10k words and 2 ASICs:
https://talesonthewire.com/projects/floating_dragon/

1

u/tehfrod 8d ago

Look for the paper "What Every Computer Scientist Should Know About Floating-Point Arithmetic" and read it. That will explain it more fully than a reddit response will.

1

u/mredding 8d ago

Why does 0.1 + 0.2 result in 0.30000000000000004 in many programming languages?

The problem is there are an infinitesimal number of fractional values; you can always split a small value into yet smaller values. When you do this over a finite space - 32 bits, for example, there's a floor to your resolution, so that smaller fractional values have to round.

Real numbers are encoded. There are a number of ways you can do this. If you wanted 0.1 + 0.2 = 0.3 exactly there ARE encodings that do support it - but they're just going to have to sacrifice something else.

IEEE 754 standardizes an encoding for scientific numbers in base 2. So 0.3 does not encode exactly. There are 2 encodings for +/-infinity, two encodings for +/-0.0 (for limits approaching zero, so you know the direction of the approach), half of all binary patterns in the encoding represent values between 1 and -1, and the other half are for all other numbers beyond. A 32 bit float also has ~16m NAN representations, a consequence of the encoding. The further away from zero you go, the larger the gap between adjacent representable numbers.

The standard also supports decimal encoding where it DOES support 0.3 exactly, and that encoding is meant to be used in finance. Scientific computing doesn't care about that.

There are other encodings, like Q1.31 or P32. These and others encode ONLY values between 1 and -1, which is where most scientific computing is located. Q1.31 spreads encodings evenly across the encoding space, so the representable values are always an equal step. P32 saves most of the encoding space for 1 to 0, and the rest grows adjacent values exponentially.

Why can’t computers represent certain decimal values precisely?

The above should suggest a bit - we can't represent an infinite number of values, and how you align those values follows a curve that give you some desirable properties and sacrifices others. What's a shame is that floating point hardware isn't programmable like an FPGA so we can just select the encoding best fit to the job and get acceleration for that. Publishing standards back in the day of fixed hardware meant portable code and predictable results, it was a different kind of solution that carries over today.

How does floating-point representation actually work at the hardware level?

I mean, look it up. The IEEE 754 standard is well understood and demonstrable. You can do it in software and affect the same results. The only thing you'll be warned about is the standard only guarantees outcomes, not implementation, so the bit twiddling hacks over IEEE 754 are not portable; you're supposed to use the API.

Or another way you can explore this is fixed point math - you can implement your own floating point in terms of integers, which is essentially what I was suggesting just before.

More importantly, how do systems where precision is critical deal with this?

Analysis, analysis, analysis.

You take steps to understand the stability and precision you can achieve out of your implementation before you go into production.

Bear in mind that VERY FEW applications need more than 6 points of precision for ANYTHING. No robot or rocket engine, very few scientific machines are going to be that dead set on higher levels of precision. And if they are, there are OTHER encodings and standards that might be a safer bet. You figure ALL this out on paper before you commit to anything in a critical system.

The general idea is that you're going to perform calculations that will inevitably accumulate error. The idea is that you get a result with more precision than you need, more precision than error, and you only use the part you need. Ideally you don't accumulate error continuously, but you can always start your computation again from some external reference point.

There are some tools you can use to manage the accumulation of error.

One thing you can do is shadow the float calculation in long double, the difference will give you a sense of how much error your computation accumulates.

You can implement - or for us mortals, USE an interval arithmetic library that tracks the amount of error with an upper and lower bound for every operation. These are built on error free transforms like Kahan summation or Dekker's TwoSum.

You can perturb your calculation - do it again for the next adjacent representable value. The magnitude of the difference between the two results tells you the magnitude of how unstable your calculation is.

You can just... FUCK with the equation, because equivalent computation expressed in another way may be WILDLY stable or unstable.

If there are other tools and techniques, and I'm damn sure there are plenty, I don't know them. I'm principally trained in video games where we can tolerate literally any amount of slop because NO ONE IS GOING TO DIE. I work in robotics now, I've got pneumatic lines at 700psi we would rather not blow off next to someone's fucking head. Or hand. And we would like people not to get pulled into and ground up in the machines. Bouncing around industries has put some perspective on me.

And the financial industry uses fixed-point math. It works, it's tried and true. I know there are these floating point decimal encodings, but I've never worked for anyone who wanted to bother. Why? because the most money I've seen disappear in an instant due to a coding error was $40m. Once. That was enough. I don't think these decimal precision types are dangerous, I'm just saying plenty of shops are slow to adopt.

2

u/nicuramar 1d ago

There are not “an infinitesimal number of fractional values”, there is an infinite number of them.

1

u/mredding 1d ago

I blame being drunk, but I cannot verify, because I might have been drunk.

1

u/W31337 8d ago edited 8d ago

Because a floating point number is built up differently. It stores numbers less precise but bigger magnitude. This gives precision errors and therefore you need an epsilon. Epsilon is basically a value range that determines equality.

Everything in engineering and physics works with tolerances including rocketry. But the main issue with rocketry is, is that the solar system is big. Rockets to other planets do mid course burns to adjust for precision issues after initial burn.

1

u/Silly_Guidance_8871 8d ago

Your three bullets really map to two questions, which I'll try to answer.

Why does 0.1 + 0.2 result in 0.30000000000000004 in many programming languages?

(The second question is really just a rephrase of this)

It's ultimately for the same reason that you can't perfectly represent 1/3 as a decimal (0.3...) where there's a finite number of digits — there's no powers x, y where 10x = 3y. Floating point numbers are fundamentally based around binary fractions, and when converting from decimal fractions to binary fractions, you need to choose a base y where 10x = 2y; the x comes from however many digits are in the decimal fraction, so that's known: 0.1 requires x = 1, and 101 gives 10 (as the denominator), and there's no integer y that lets us solve 2y, so there's a remainder. That happens to lead to an infinite-length binary fraction, but you don't have infinite binary digits — you typically only have 24 (for 32-bit floats), or 53 (for 64-bit floats), but more on that below. As a result, there's a rounding involved to fit into a fixed-sized binary fraction, and it so happens that the typical default rounding mode is often not "balanced" — as an example, Javascript's Math.round() uses "round toward positive infinity". This works fairly well with decimal fractions, but usually "round toward nearest even" is a generaly better choice with binary fractions (IEEE754 specifies a bunch of rounding modes for float operations). But, Javascript (and a lot of other languages) often had their first versions (effectively) slapped together, and now there's a standard to uphold that would break existing code if it changed.

How does floating-point representation actually work at the hardware level?

At its core, floating point is literally just scientific notation, but using binary fractions/exponents instead of decimal. Much as you can have 1.234e2 to represent 123.4 with decimal fractions, you can have 1.001101e101 (note: it's all binary) to represent 100110.1 with binary fractions. In either case, the number has a sign (positive/negative), a mantissa (the "significant digits"), and an exponent (positive or negative). And unless the value is zero, the leading digit/bit is not allowed to be 0 (the exponent is adjusted to enforce this invariant): 0.23e0 is not valid scientific notation; 2.3e-1 is.

Historically, there were two well-supported sizes of floating-point numbers. Each has 1 bit reserved for its sign (0 = positive, 1 = negative), and both positive and negative zero are permitted (and considered equivalent). Also, since all non-zero floats have the leading bit of their mantissa set, that bit can be elided, so you effectively get one more bit of precision than the raw number of bits would imply.

  • 32-bit (single-precision): 23-bit mantissa (effectively 24); 8-bit exponent
  • 64-bit (double-precision): 52-bit mantissa (effectively 53); 11-bit exponent

The exponent rises more slowly than the mantissa because it was determined that for most things, better precision mattered more than extreme range. Despite the apparent "smallness" of double-precision's 11-bit mantissa, when it's all combined, it can represent numbers up to around +/-1.8e308 decimal (which is a fairly damn big number).

--------

Lastly, floating point has allowances for positive/negative infinity, as well as not-a-number. And "denormals", if you're a sadist. Infinities work around the "wrapping" problem of most integer implementations by turning it into a singularity (oops, too big, guess it's infinite). Not-a-number is for cases like divide-by-zero. Denormals let you (effectively) sacrifice some mantissa bits for extra exponent bits, but aren't always supported, and are generally very slow to compute.

1

u/pickle9977 8d ago

Between any two floating point numbers, no matter how close they are digitally in the lowest value place have an uncountably infinite number of values between them that we cannot represent and we handle via truncation.

What gets truncated is accumulated across operations as value error and depending on the values being operated on and the operations performed and in what order that error can grow large enough that the result output is meaningfully incorrect in relation to the expected value were truncation not to occur.

So you can gets some really weird results in very specific conditions but most of the time it’s good enough.

1

u/Adorable-Strangerx 8d ago

More importantly, how do systems where precision is critical deal with this?
For example, spacecraft navigation, rocket guidance systems, orbital calculations, and scientific simulations require incredibly accurate computations.

Just use integer, array of chars, or something like that. You won't have floating point issues if you don't use floating point.

1

u/zhivago 8d ago

Once you understand that a floating point value denotes a range, not a number, your question evaporates. :)

1

u/gm310509 7d ago

The best resource is https://0.30000000000000004.com/ but try to think of this little thought experiment.

If I asked you to write down the answer to 1 ÷ 3 as a real number (i.e. 0.33333333) and limited you to some number of digits - it doesn't matter how many digits I limit you to whether it is 4 digits, 10, 100, 1 million, it still results in the same outcome.

So. to 4 decimal places, 1 ÷ 3 = 0.3333.

Next, multiply that by 3, what do you get? The answer is 0.9999 - and that, albeit in a different context, is what is happening. The difference is that this example is in decimal, the computer is using binary. The same "problem" exists in binary, but just for different values.

1

u/cdsmith 7d ago

Floating point numbers are in base 2. That means that the only fractions you can represent in finitely many bits are those for whom the prime factors of the denominator are just 2s. In other words, the denominator must be a power of 2. Anything else, like 1/10 for example, repeats infinitely, so with finitely many bits you just get an approximation.

By contrast, decimal numbers are written in base 10, and the prime factorization of 10 is 5 * 2. That means the only fractions that you can represent in finitely many decimal digits are those for whom the prime factors of the denominator are 2s and 5s. So, for instance, 0.1 = 1/10, 0.04 = 1/25, 0.5 = 1/2, etc. Anything else, like 1/3 for example, repeats infinitely, so with finitely many decimal digits, you just get an approximation.

Consequences:

  • Any valid floating point number (finitely many base 2 bits) can be represented exactly in either base 2 or base 10.
  • However, there are some finite decimal numbers which can not be represented exactly in base 2. Namely, those whose fraction form has 5 as a factor of the denominator.

To make things a little more confusing, programming languages typically do NOT print the exact mathematical value of a floating point number. If they did, then just printing out 0.1 as a float by itself would result in 0.1000000000000000055511151231257827021181583404541015625. That's the number that can actually be represented in a float, with the denominator as a power of 2. What many languages print instead is the shortest decimal number that will round to that floating point value if you convert it.

But when they do math, they do it with the exact mathematical value, so the rounding error accumulates. Here, notice that "0.1" converts to a float that's slightly larger than the number 0.1, and similarly, "0.2" converts to a float that is slightly larger than the number 0.2. When you add them, the rounding error adds up. And crucially, the result has just enough error that it's no longer exactly what 0.3 would round to. It's one "unit of least precision" (known as an "ulp") greater. When you print it out, many programming languages will therefore no longer shorten it to 0.3. Instead, they will print the shortest decimal that would round to that slightly larger value, and that is 0.30000000000000004 (Note that this isn't the actual value of the floating point number, which would be much longer. It's the shortest decimal string that rounds to that actual value.)

1

u/billsil 7d ago

What does it matter what the precision is when your flight computer is running at 100 Hz? Your fancy grid fins only have a bandwidth of 20 Hz.

What happens when you have a wind gust that knocks you slightly off course or your engine can’t gimbal at 10,000 Hz to deal with the acoustic load driven by vehicle asymmetry due to your raceway (the racing stripe down a rocket that passes electrical and plumbing). 

Nothing can react that vibrational load out, so they don’t try. You design a robust control law that can follow the expected trajectory given large amounts of noise. The precision on floating point math is more accurate than you’ll ever need. You don’t even need 64 bit architecture and it’s actually better from a radiation hardness perspective.

1

u/iqisoverrated 7d ago

Really depends on the calculation. Flight control systems will recalculate from current sensor data constantly so the error doesn't 'add up' and an error after so many decimal places doesn't really impact real world operations (because all your motors and regulators and whatnot also only have a finite granularity...and your sensor input values have a granularity/noise level, anyways, so doing 'more precise math' doesn't really help beyond a certain point.).

In the end your system doesn't have to be perfect. It just has to be good enough to satisfy the use case/requirements....and the range of what 'good enough' means for your particular application gives you an indication how precise your calculations need to be.

1

u/bartekltg 7d ago

> Why does 0.1 + 0.2 result in 0.30000000000000004 in many programming languages?

Floating point numbers are (simplifying the technical details) an integer times 2^integer (both integers are from a limited range). So, 456 or 825/1024 are perfectly fine.
This is the same as in decimals. If you can use only a finite number of digits, you will have integer * 10^ integer.
so 24, 1345000000 or 34/10000000 are fine, but 1/3 is not.

> Why can’t computers represent certain decimal values precisely?

They can.
But for most cases, the short decimal fraction aren't more important than any more important than other numbers. Why 2/5 need to be precise, but 1/3, 1/7 or pi does not?
Unless you deal with currency (then you just keep amount of cents or 1/100 of a cent in an integer) you just need to know that your result is so and so close to the real result. How many digits of pi do you use in your calculations? How precise did you measured the diameter of your table? All calculations you did that was not on fractions/whole numbers was done the same way: you do not care about 4th or so digit.
That <10^-15 precision is, with a handful of exception, more than you get from any measurements of physical stuff.

> How does floating-point representation actually work at the hardware level?

This is a rabbit hole. And there is many great sources in the internet. TLDR: you have three parts. Sign. + or -. A some bits that represent a fractional part. And some bits that represent a exponent. The value is
sign * 1.fractional_part * 2^ (exponent - constant) (at leat for normal numbers, there are also special values and subnormal numbers...).
But if you do not care about bits, it is just a*2^b. a is some-bit integer, b is some(less) bit integer.
When you do an operation (+,-,*,/, but also some simple functions, like sqrt) as a result you get the closest value the floating numbers can represent to the correct value of the operation (performed on the inputed numbers).
Tiny transistors do the work, but it is done the same way as you would. a*2^b * c*2^d = a*c *2^(b+d) or you are shifting the smaller exponent when you add.

> More importantly, how do systems where precision is critical deal with this?

The science that deal with those algorithms is called numerical analysis and a part of it is to see how floating points number (so, a limited precision of numbers and operations) influence the results. And we use algorithms that are "good" (there is a couple kind of "good" starting with forward and backward stable) and control for errors. And if we really need, we use more precise numbers (but it is quite rare. Like energy levels in atoms that we know very precisely, and matrices that allow calculations are numericaly ugly).

There is a fresh funny video about even uglier matrices;-)
https://www.youtube.com/watch?v=T7qQzQsYb_c
It also mention some concepts, like grow factor and condition number.

But, again, this is the result of a limited precision, not that you can't write 0.1 exactly.

> For example, spacecraft navigation, rocket guidance systems, orbital calculations, and scientific simulations require incredibly accurate computations.

Lets say we want to travel to the Moon, and simulate it in the earth reference frame. In cartesian coordinates (this is not how you do it, but lets not complicate). The point 0,0 is the center of the Earth.

The rocket sits on the surface, so it is ~6370km away. The precision of the "doubles" is 1.11 *10^-16. So, the number allow us to describe the position of the rocked with 6370km*1.11*16^-16 = ~7.1*10^-10 m. This is like twice the diamater of the carbor atom (not the distance between )
It landed on the moon, now our numbers allow for precision of 44nm. It would be a bit hard to measure with laser interferometry (the visible light is 400-800nm).
Lets put that rocket on the Jupiter: 0.08mn.
On not_exactly_a_planet Pluto: 0.6mm.

(fun fact, a game Kerbal Space Program initially used single precision numbers for its calculations, but had to change to double precision).

Numbers alone are precise enough. But here come problems:
The error accumulates. The rocket trajectory is calculated in steps. It literally simulates the movement timestep after another. Each step introduces another numerical error. How those error accumulates depend on the problem. Orbital mechanics in short span is quite forgiving, lets say the accumulation is linear. We id a million steps. We "add" 6 zeros to each. It is still too precise to measure on the Moon, but we get kilometers of error on the simulated trip to Pluto. But it doesn't mater because of the second problem:
Each of those steps is not getting a perfect results even in exact arithmetic. It is an another rabbithole. TL:DR each step we introduce error. Those errors are smaller for smaller steps, but they are much larger than the error from number representation! More precise number would not help us here, without also calculating everything in insane number of steps.

But it turns out that precision would be useless. A rocket is a pipe with violent chemicals, we cant guarantee it wont be 1% weaker or stronger at that or this part of the trip. We have a limited ability to control how long a corection burst takes. So we already need to measure constantly and do corrections. (measure is another important aspect, in the simulation the rocket "knows" where it is, in reality id doesn't).

As a side note, there are system of ordinary differential equation that are much worse. Lorenz System is just 3 equations. A bunch of balls colliding on a billiard table. Solar system in long term also seems to a bit chaotic. In such systems any initial change will grow and after some time those initially similar trajectories will spread apart. And NO precision level will save us from chaos. (often dealing with such system, like simulating tons of molecules, we do not care about the exact trajectory, but rather global properties. So we use for example Verlett methods in Molecular dynamics. It is less precise that high order methods, but it is simpletic (can preserve some important physical properties)).

> Do they use higher-precision floating-point formats (64-bit, 128-bit), arbitrary-precision arithmetic, fixed-point arithmetic, or entirely different techniques?

64bit (double precision) is essentially default for science stuff (with exceptions, like molecular dynamics mentioned above).

Higher precision is used when needed. My friend worked on those energy levels in atoms and they used double double, quad double (those are numbers that cleverly use 2 and 4 double precision numbers to work together as one, getting a bit less than the precision of 128 and 256 bit floating point numbers) and MPF/MPFR (libraries for arbitrary precision, they used it on the shorter range, to get a bit more than quad double). But again, the measures are <10^-10, the condition numbers are awful (10^10), and you want to see small effects in that ;-)

But again, it is rather rare. Most of the time the need (the experimental results you can compare) and the strength of the numerical method itself limits us more than the precision of the numbers.

> I’d love to understand how engineers manage numerical precision in real-world, high-stakes systems.

Do not use numerical unstable methods.
If you get a specification that tells you to make a engine turbine with precision greater than the size of a proton, someone don't know what they are doing ;-)

More seriously, you have to be aware of it and control for problems. This is why numerical science is a bit more than just learning hot wo use numpy and matlab.

(and the last time, I want to emphases that all the things I have written in most of this too long comment has nothing to do with the imprecision of small decimal fraction. If we would use base 10 in computations, every mentioned challenge would still appear, just the calculations would be a bit slower)

1

u/cthulhu944 7d ago

Floating point is obviously a way to represent real numbers (as in the real number set) using binary. The interesting thing is that some numbers that are rational in a decimal representation are irrational in binary representation. In either base, an irrational number represented with a finite set of digits is always an approximation. Any math you do on the approximate value is likely to be off by some small amount.

1

u/pvera 7d ago

The navigation question is simple once you realize that it knows where it at all times, this is because it knows where it isn't.

1

u/johndcochran 7d ago

I made a rather long post explaining this a while back. Take a look at this post I made previous. It ought to answer your question.

Hint: There isn't a floating point error. That answer is quite correct.

1

u/PaulRudin 6d ago

In addition to what others have said - computers can absolutely represent decimal values to arbitrary precision. But most computers have special hardware that's much faster with the usual representation of floating point numbers.

1

u/SagansCandle 9h ago

Rockets (and banks) often use fixed-precision integers (aka Decimals) where precision matters.

123456 @ 2 decimal places = 1234.56

0

u/victotronics 8d ago

Just talking fractions, computers only have 1/2, 1/4, 1/8, 1/16, ... to work with. So 1/3, 1/10, 1/pi all have to be approximated.

How do systems deal with this? Use algorithms that are tolerant to errors. That's a serious mathematical discipline. You need to take at least a semester of classes to really understand this. Find the books by Wilkinson to get an impression of how tricky it is. One of the big open questions was only settled the other day: the "growth factor" in Gaussian elimination.