Arbitrary Precision Floating point numbers #452
Replies: 3 comments
-
Neat stuff. This is all new to me. Are there any typical use cases that demonstrate the power of arbitrary precision floating point numbers the same way you used the Ackermann function to demonstrate the arbitrary precision integer class? Also curious, is your design based on what you read in the IEEE 754 Wikipedia page, or some other inspiration? Cool stuff. |
Beta Was this translation helpful? Give feedback.
-
The way I have everything coded, a user can extend a base class to include any number of exponent bits and significand bits. See the minimal amount of code in the Octuple class for example. The library currently has implementations that begin to follow the IEEE standards. When I say "begin to" it's because they use the same number of bits. I have not begun looking at the infinities, positive versus negative zero, subnormal numbers or the two types of NaN. Other edge conditions may not work right yet, but the framework is in place. As far as use cases, obviously more bits means more precision. Poorly converging series can be calculated more precisely with less floating point error for example. |
Beta Was this translation helpful? Give feedback.
-
The classes are now able to be created from a native float and integers expressed as strings, and multiplication and division work. |
Beta Was this translation helpful? Give feedback.
-
After implementing arbitrary precision integers, I thought I'd tackle Floats. The problem with floats is that most fractions are infinitely repeating in base 2, so there always has to be some rounding. I've decided to allow users to specify the number of bits assigned to the exponent and the significand in their own objects, and make the math algorithms generic enough to operate on any design. I've included IEEE Single, Quadruple, and Octuple precision floats since these are standards, and also to prove the math works on objects with differing designs.
As far as I know, there are no Quadruple precision Float libraries for PHP. BCmath is probably the closest thing.
If you have time, glance it over and let me know if you have any nits to pick.
https://github.com/Beakerboy/math-php/blob/OctuplePrecision/src/Number/Octuple.php
It can currently add and subtract integers. You can also create a float from any binary string (like unpack). Also the comparison operators work for any object, not just integers.
It turns out decimal string to binary float is a Very Hard™ problem in computer science, along with the opposite, displaying a float as a human readable decimal.
Beta Was this translation helpful? Give feedback.
All reactions