Is there any simple way to get the fraction part out from a double?
The result must be a double as well.
Fraction part of a double
Code: Select all
Alpha . d = 123456.789
Omega . d = Alpha - Int ( Alpha )
Debug OmegaAnother way using Round():
Code: Select all
doub.d = 3.14159265
Debug doub - Round(doub, 0)Windows 7 & PureBasic 4.4
Procedure Fraction
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
Re: Procedure Fraction
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
If so, your code doesn't handle that properly, but eesau's does:
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)
EndProcedurefraction of a double
Mathematically speaking you're right. But Since the integer part of the number shows the signum (+/-) of the number there's no need for the fractional part of the same number to show this.
- Kaeru Gaman
- Addict

- Posts: 4826
- Joined: Sun Mar 19, 2006 1:57 pm
- Location: Germany

