Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Convert string 1/4 or 1/2 to numeric
Message
De
20/01/2016 05:50:51
Lutz Scheffler
Lutz Scheffler Software Ingenieurbüro
Dresden, Allemagne
 
 
À
20/01/2016 05:41:08
Dragan Nedeljkovich (En ligne)
Now officially retired
Zrenjanin, Serbia
Information générale
Forum:
Visual FoxPro
Catégorie:
Autre
Divers
Thread ID:
01629854
Message ID:
01629975
Vues:
42
>>>>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.

My code is only valid for fractions that have nominators in powers of 2. 1/2; 1/4;/1/8;...
So 0.1 is not a valid paramater.
Bill only likes to transform those, and for that you only need the number of digits.
IOW decimal fraction of 1/(2^n); n in {0;1;2;3;..} will fit into n decimal places

Edit:
There should be no precision problem. numbers base 10 and base 2 both can store such fractions wihout any loss in precision (IOW they will not become periodic), since both work with 2 in there base.
Words are given to man to enable him to conceal his true feelings.
Charles Maurice de Talleyrand-Périgord

Weeks of programming can save you hours of planning.

Off

There is no place like [::1]
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform