5 ms·
How do you compute the midpoint of an interval? (2014) [pdf]
- sova 8y agoTake an average in a range! Neat. Floating point uses exponential indexing and gets blurry. Taking a chunk of an exponential range and averaging that seems to solve the inaccuracy (!)
- deleted 8y ago[deleted]
- jacobolus 8y agoSummary: to compute (a + b) / 2 and get the best answer for arbitrary inputs, you want: 0 if a == –b ±realmax if a or b is ±∞ otherwise compute (a - a/2) + b/2 at higher precision, then round to the nearest float
- Someone 8y ago”±realmax if a or b is ±∞” That, IMO, is debatable. NaN is an option, too (as it is for the case a=+∞, b=-∞, so one can argue ”0 if a == –b” needs tuning, too) Both choices break the property m(I) ∈ I, but that may be fine, as NaN doesn’t mean the midpoint isn’t in the interval, but just that one cannot tell where in the interval it lies.
- thaumasiotes 8y agoIs a == -b true when a and b are +inf and -inf? That's not what I would have expected.
- jacobolus 8y agoYou are asking if ∞ == ∞? Yes.
- wutbrodo 8y agoIt's been years since I got my math degree, but IIRC this isn't true. At least, it's not true in enough framings that you can assert it this plainly. Infinity isn't just another number, and just because the symbols are identical in either side of the equals signs doesn't mean the entities described are.
- jacobolus 8y agoWe are talking about the floating point number format and the behavior of computer programming languages. In “mathematics”, the meaning of the symbol ∞ depends on what number system we are talking about. In some contexts ∞ is not a “number”, but rather a shorthand for a statement about limits. In other contexts it is a number. If you want to learn about the “affinely extended real numbers”, which is what floating point numbers more or less approximate, you can read https://en.wikipedia.org/wiki/Extended_real_number_line https://en.wikipedia.org/wiki/Extended_real_number_line
- fhars 8y agoSimilar things are relevant for calculations with integers (like array indexes), if you haven‘t yet, read this classic post about it: https://ai.googleblog.com/2006/06/extra-extra-read-all-about-it-nearly.html?m=1 https://ai.googleblog.com/2006/06/extra-extra-read-all-about... (reader mode makes it accessible on iPhones, why don‘t people test their layouts with common browsers?).
- Sean1708 8y agoIn C and C++ (where you don't have the >>> operator), you can do this: 6: mid = ((unsigned int)low + (unsigned int)high)) >> 1; Since you're casting to unsigned int anyway, can't you just use / 2?
- kevinventullo 8y agoYeah, I believe those compile to the same thing for unsigned ints. You can confirm using godbolt.
- abhinai 8y agoThis post is a great reminder of just how much we need to respect the people who write or have written system libraries. Even simple things can be so complicated when you have to factor for all eventualities. A rocket could blow up because the library function you wrote didn't account for floating point arithmetic correctly.
- adwn 8y ago> A rocket could blow up because the library function you wrote didn't account for floating point arithmetic correctly. I would go so far as to say that using floating point in any software that might cause the rocket to blow up is probably Doing It Wrong (TM). That does not include the video encoder sending a livestream to the ground station, but it does include the µC that controls the engine turbopumps.
- lhjuhafjbh 8y agoA microcontroller for a pump probably doesn't need floating point numbers, but flight control software uses it. This went wrong before and an Ariane 5 was blown up (automatically as a safety measure), see https://en.wikipedia.org/wiki/Cluster_(spacecraft)#Launch_failure https://en.wikipedia.org/wiki/Cluster_(spacecraft)#Launch_fa... Strictly speaking the problem was converting floats to 16-bit integers which ended in an overflow... So avoiding floats doesn't even help as it won't protect you from overflows.