Comments (11)
Hi, what would be the difference between getScale()
and getPrecision()
?
from math.
hi @BenMorel
The PostgreSQL manual will say it better than me:
We use the following terms below: The precision of a numeric is the total count of significant digits in the whole number, that is, the number of digits to both sides of the decimal point. The scale of a numeric is the count of decimal digits in the fractional part, to the right of the decimal point. So the number 23.5141 has a precision of 6 and a scale of 4. Integers can be considered to have a scale of zero.
from math.
@BenMorel any interest? thanks!
from math.
Working with precision is generally better than working with scale
Check this example:
(0.03/360)*8*30 = 0.02
Woking with scale of 10:
0.03/360 = 0.0000833333(3)
0.0000833333*8=0.0006666664(0)
0.0006666664*30=0.0199999920(0)
❌ 0.0199999920 == 0.02
Working with precision of 10:
0.03/360 = 0.00008333333333(3)
0.00008333333333*8=0.0006666666666(40)
0.0006666666666*30=0.01999999999(80)=0.02
✔0.02==0.02
from math.
@alexviar i'm not sure what your example shows but it's not precision of 10
Working with precision of 10:
0.03/360 = 0.00008333333333(3)
0.00008333333333(3)
has as precision of 16
from math.
@alexviar i'm not sure what your example shows but it's not precision of 10
Working with precision of 10:
0.03/360 = 0.00008333333333(3)
0.00008333333333(3)
has as precision of 16
It has a scale of 14 but a precision of 10. The precision refers to how many significant digits it has, not how many decimal places it has. https://www.logicbig.com/quick-info/programming/precision-and-scale.html
For example Big.js uses scale whereas Decimal.js uses precision
from math.
@alexviar i'm not sure what your example shows but it's not precision of 10
Working with precision of 10:
0.03/360 = 0.00008333333333(3)
0.00008333333333(3)
has as precision of 16
I did put in parentheses digits that was rounded, and underlined digits are the Periodo of decimal
from math.
ah right thanks, i forgot that leading zeros are not significant.
but also for storage purposes (postgres), 0 <= scale <= precision so, It has a scale of 14 but a precision of 10.
doesn't work, in practise.
anyway, the point of this of was to simple add a getPrecision()
method, similar to getScale
, nothing mnore
from math.
Hi @bendavies, sorry for the late reply. I'm not against adding a getPrecision()
method if it is useful to you, so please feel free to open a PR along with a few tests, in particular to clarify what happens for leading zeros.
@alexviar I'm not sure what the decimal period has to do with the precision? Anyway, you make me think that this would be a great addition to BigRational
: a way to output 10/3
as 3.3(3)
for example. Would you want to work on this?
from math.
I opened issue #74 to track the BigRational as decimal number with period separately.
from math.
@alexviar I'm not sure what the decimal period has to do with the precision?
I think I didn't explain myself well. I did put rounded decimals (and decimal period) in parentheses for illustrative purposes only. Precision is given by working with n significant digits instead of n decimal place:
Operation | Rounded to 10 significant digits | Rounded to 10 decimal places |
---|---|---|
0.03 | 0.03000000000 | 0.0300000000 |
360 | 360.0000000 | 360.0000000000 |
8 | 8.000000000 | 8.0000000000 |
30 | 30.00000000 | 8.0000000000 |
0.03/360 | 0.00008333333333 | 0.0000833333 |
(0.03/360)*8 | 0.0006666666666 | 0.0006666666 |
(0.03/360)*8*30 | 0.02000000000 | 0.0200000000 |
Now, if we take the rounded values to do subsequent operations, we have:
Operation | Rounded to 10 significant digits | Rounded to 10 decimal places |
---|---|---|
0.03 | 0.03000000000 | 0.0300000000 |
360 | 360.0000000 | 360.0000000000 |
8 | 8.000000000 | 8.0000000000 |
30 | 30.00000000 | 8.0000000000 |
0.03/360 | 0.00008333333333 | 0.0000833333 |
(0.03/360)*8 | 0.0006666666666 | 0.0006666664 |
(0.03/360)*8*30 | 0.02000000000 | 0.0199999920 |
Currently it can be circunvent by using BigRational
, but this workaround doesn't work with irrational opreations like square root for example ( Never mind, I had read an article with an example like this, but I can not found it)sqrt(2)*sqrt(2)
from math.
Related Issues (20)
- brick/math method for gmp_random_range HOT 4
- brick/math method for gmp_invert HOT 8
- The given value ".1200" does not represent a valid number. HOT 2
- Add a changelog HOT 3
- Rounding error on exactlyDividedBy() HOT 2
- Fatal error: Maximum execution time of 30 seconds exceeded HOT 2
- 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
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.