Hello world. This is my code.
import math
var
num = 786.654
intPart = floor num
decPart = num - intPart
echo decPart #got 0.6539999999999964 instead of 0.654
I get worse results when I add digits to the decimal part. Please, is there any reliable way to extract the decimal part of a number ? I need it for a multiprecision library.Because num is being converted to binary (base2) from decimal (base10) in the source code, you are seeing a artifact of decimal-to-binary conversion.
One work-around is to store the decimal numbers in decimal form. My library, decimal128 does this but a floor function as not been written for the library. (A lot of things have not been written for it yet.) A work-around is to convert it to an integer temporarily, like so:
import decimal128
var
num = newDecimal128("786.654")
intPart = num.toInt
wholeNumber = newDecimal128(intPart, scale=3)
decPart = num - wholeNumber
echo decPart
Details at https://nimble.directory/pkg/decimal128
Try:
import rationals, math
var
num = 786.654
ratNum = num.toRational
numPart = floor num
decPart = ratNum - numPart.toRational
echo numPart
echo decPart.toFloat
If the decimal is important, it's usually preferable to work with big integer instead due to floating-point rounding error.
Yes, it seems that in French “partie décimale” is more frequently used, but “partie fractionnaire” is also acceptable:
https://fr.wikipedia.org/wiki/Partie_enti%C3%A8re_et_partie_fractionnaire#Partie_fractionnaire
Some purists say that “fractional part” is inadequate as, for an irrational number, this part is not rational and thus cannot be a fraction; but “decimal part” is no more adequate as it applies only to decimal numbers.
I don’t remember what term was used when I learned mathematics long time ago. But, now, I used “partie fractionnaire” in French and “fractional part” in English. That seems more correct.
Decimal is just a number base and a form of textual encoding ... the term is meaningless/wrong if your numbers are printed in hex or binary, for instance. The fractional part, OTOH, has a mathematical definition and importance.
Some purists say that “fractional part” is inadequate as, for an irrational number, this part is not rational and thus cannot be a fraction;
Those aren't purists, they are simply ignorant--"fraction" doesn't have anything to do with being rational. a/b where a and b are integers is a rational fraction, but not all fractions are rational. In any case, since computers have finite precision and we're not doing symbolic arithmetic, all these values are rational.
It's really not, as can be seen by reading that Wikipedia article past the first six words.
The problem is that in these six words, fractional part and decimal part are presented as synonyms. So, this is bad wording. And, actually, never in the article, decimal part is really defined.
There are other sites which provides clear definitions as here: https://www.math-only-math.com/decimal-numbers.html
The French Wikipedia article is not better and define also both terms as synonyms. But we can find this sentence on decimal part (translated in English):
“We find also the term decimal part of the number, in particular for decimal numbers.”
Note the “in particular” which I find odd.
In fact, I think that it is generally a bad idea to use Wikipedia as a reference. There are too many inaccuracies and inconsistencies.
Those aren't purists, they are simply ignorant--if you know such people, have them read the Wikipedia article archnim cited
I don’t know such people; this is what is said in the French Wikipedia article on fractional part. It cites Scolab as a source, an educative site which indeed uses very strange definitions. The translated sentence from Scolab states this (with no more explanations):
“In the case of an irrational number, we cannot speak of a fractional part.”
"fraction" doesn't have anything to do with being rational. a/b where a and b are integers is a rational fraction, but not all fractions are rational.
This is the problem with Wikipedia. Some writer found that the definition given was wrong and has added a sentence to provide Scolab point of view. In the French Wikipedia text, “fraction” is in fact understood as elements of the fractional numbers set which is in fact the set of rationals. This is what English Wikipedia names “common fraction”.
In any case, since computers have finite precision and we're not doing symbolic arithmetic, all these values are rational ...
Of course. When I was a young engineer, in the 80s, I discovered this, the hard way, and there was no Wikipedia to help me 🙂.
oh man, if only human languages had a spec and were explicitly defined.
goddamn meatlings and their continual uses of metaphor and metonymy
Besides reliable, what requirements do you have?
If you want something generic use trunc (https://nim-lang.org/docs/math.html#trunc%2Cfloat32).
If you want something fast, extract the mantissa bits from the float and shift by the exponent bits.
Example:
Good luck to support big-endian machine in that case though, and make sure to test with denormal, NaN, -Inf, +Inf