Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Convert string 1/4 or 1/2 to numeric
Message
From
20/01/2016 05:41:08
Dragan Nedeljkovich (Online)
Now officially retired
Zrenjanin, Serbia
 
 
To
20/01/2016 03:38:50
Lutz Scheffler
Lutz Scheffler Software Ingenieurbüro
Dresden, Germany
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
01629854
Message ID:
01629974
Views:
59
>>>There shoud be a way using LOG10() without counting numbers, but I'm to lazy right now. (For the whole part it's INT(log10(ABS(EVL(INT(tnNumber),1))))+1 )
>>
>>Understood!
>>Thank you.
>
>Hi Bill,
>
>I did not found a simple way to calculater the decimal digits wit LOG.
>So the kludge for stuff based 2: (I had some fun with it):
>
>CLEAR
>
>?GetCommonFract_2(1)
>?GetCommonFract_2(0)
>?GetCommonFract_2(13/32)
>?GetCommonFract_2(0.25)
>?GetCommonFract_2(9/16)
>?GetCommonFract_2(31/16)
>?GetCommonFract_2(-15.125)
>
>FUNCTION GetCommonFract_2
> LPARAMETERS;
>  tnNumber
>
> LOCAL;
>  lnWhole,;
>  lnFrac,;
>  lnNominator
>
> llSign      = SIGN(tnNumber)=-1
> tnNumber    = ABS(tnNumber)
> lnWhole     = INT(tnNumber)
> lnFrac      = tnNumber-lnWhole
> lnNominator = 2^(LEN(RTRIM(STR(lnFrac,15,15),"0"))-1)
>
> RETURN IIF(llSign,'-','')+;
>  IIF(EMPTY(lnWhole),IIF(EMPTY(lnFrac),'0',''),LTRIM(STR(lnWhole,15))+' ')+;
>  IIF(EMPTY(lnFrac),'',LTRIM(STR(lnFrac*lnNominator,15))+'/'+LTRIM(STR(lnNominator,15)))
>ENDFUNC &&GetCommonFract_2
>
>
>Edit: added parenthesis on 2^....

Wouldn't
lnNominator = 2^(int(LEN(RTRIM(STR(lnFrac,15,15),"0")*log(10)/log(2)))+1)
be more correct? Because for three decimal digits you need two to the power of ten to have your error below 0.001... and 3*log(10)/log(2) is 9.9658, and the +1 there gives you 10.

back to same old

the first online autobiography, unfinished by design
What, me reckless? I'm full of recks!
Balkans, eh? Count them.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform