Comments (2)
Hi, these discrepancies appear because you're using floating-point values:
echo $a = BigDecimal::of(0.00001); // 0.000010 (!)
echo $a->getUnscaledValue(); // 10
echo $a->getScale(); // 6
As advised in the README, you cannot trust a floating point value, and should always instantiate from an integer or string value:
echo $b = BigDecimal::of('0.00001'); // 0.00001
echo $b->getUnscaledValue(); // 1
echo $b->getScale(); // 5
Note that in this case, only the scale differs, the values are still equal:
var_export($a->isEqualTo($b)); // true
Where does the extra digit come from?
In this particular case, the reason is not a rounding error (a common issue with floating point numbers), but the fact that the value is converted to a string, then parsed:
echo (string) 0.00001; // 1.0E-5
The fact that it returns a number with value 10
and scale 6
is consistent with the fact that for the number 1.1E-5
, one would expect value 11
and scale 6
.
On the other hand, 1E-5
would return what you expect:
echo BigDecimal::of('1E-5')->getUnscaledValue(); // 1
echo BigDecimal::of('1E-5')->getScale(); // 5
In summary, no bug here, just use strings! π
from math.
@BenMorel Thank you for your replyοΌ
from math.
Related Issues (20)
- Rounding error on exactlyDividedBy() HOT 2
- Fatal error: Maximum execution time of 30 seconds exceeded HOT 2
- RFC: add `getPrecision` method. HOT 11
- Issue with dividedBy and MultipliedBy? HOT 3
- Feature Request: Implement lisachenko/z-engine HOT 1
- BigComplex HOT 2
- How to accept any scale without rounding HOT 1
- Accept hexadecimal input HOT 1
- Rounding is necessary to represent the result of the operation at this scale. HOT 2
- [0.10.0] behavior change HOT 1
- Please support php8.1 HOT 1
- Implements the Serializable interface, which is deprecated HOT 1
- `gmp_export` equivalent HOT 6
- Rounding bug, or am I just dumb? HOT 4
- Output a BigRational in decimal form with period
- Implement `__debugInfo` magic method HOT 4
- Add some generic number type when you don't know if the result will be `int` or `decimal` HOT 1
- BigDecimal's scale should be zero when all fractional digits are zero HOT 2
- MathException extends Exception
- When installing math i got this massage HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from math.