Fraction part of a double
Posted: Mon Mar 31, 2008 3:05 pm
Is there any simple way to get the fraction part out from a double?
The result must be a double as well.
The result must be a double as well.
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
Alpha . d = 123456.789
Omega . d = Alpha - Int ( Alpha )
Debug OmegaCode: Select all
doub.d = 3.14159265
Debug doub - Round(doub, 0)Code: Select all
Procedure.d Fraction (innumber.d)
absnumber.d = Abs (innumber.d)
fraction.d = absnumber - Round (absnumber, 0)
ProcedureReturn fraction.d
EndProcedure
Wouldn't the fractional part need to match the sign of the number (i.e. Fraction(-3.14) = -0.14) ?Capella wrote:Thanks. I realised that there was no inbuilt function for this so I wrote a procedure as follows...
...since it must work with negative numbers as well.Code: Select all
Procedure.d Fraction (innumber.d) absnumber.d = Abs (innumber.d) fraction.d = absnumber - Round (absnumber, 0) ProcedureReturn fraction.d EndProcedure
Code: Select all
Alpha . d = 123456.789
Omega . d = Alpha - Int ( Alpha )
Debug OmegaCode: Select all
Procedure.d Fraction (innumber.d)
ProcedureReturn innumber - Int (innumber)
EndProcedure