Re: Fractional part of decimal value.
Jan Wielemaker <[email protected]>
| Newsgroups | gmane.comp.ai.prolog.swi |
|---|---|
| Message-ID | <[email protected]> |
On 08/07/2013 05:15 PM, Carlo Capelli wrote:
> Hi Wouter
>
>
> when I had to compute numbers with fixed precisions (for instance,
> presenting prices after taxes or discounts) I resorted to 'hardcode' the
> precision in this way (here 2 decimal digits - actually � cents)
>
> ...
> TwoDec is round(NoIva * 100) / 100,
> ...
>
> or, using lambda:
>
> ...
> FmtNum = \I^O^(T is round(I * 100) / 100, atom_number(A, T),
> replace_word('.',',',A,O)),
> ...
> call(FmtNum, DeltaG, DeltaF),
> call(FmtNum, Rate, RateF),
> call(FmtNum, Ivato, IvatoF),
> call(FmtNum, Cost, CostF),
Is this any different from this (assuming you do the . --> , for
locale purposes)?
LC_NUMERIC=nl_NL.UTF-8 swipl-win
1 ?- format(atom(X), '~2:f', 1/3).
X = '0,33'.
(note the swipl-win :-)
Cheers --- Jan
> ...
>
> HTH
>
>
>
> 2013/8/7 Wouter Beek <[email protected]>
>
>> Hi all,
>>
>> I want to extract the integer and fractional part of a decimal value:
>> ~~~{.pl}
>> decimal_parts(D, I, F):-
>> I is floor(D / 1),
>> F is D - I * 1.
>> ~~~
>> What I get is the fractional part of the float value 1.1:
>> ~~~
>> ?- decimal_parts(1.1, _, F).
>> F = 0.10000000000000009.
>> ~~~
>> I do not mind the padding zero's (just a notational difference), but I do
>> not need the 9 at the end.
>>
>> Is there a way to get the same result as with e.g. C's modf (sample code
>> below)? Thanks for any suggestions!
>>
>> ---
>> Cheers,
>> Wouter.
>>
>> Sample code in C:
>> ~~~{.c}
>> #include <stdio.h>
>> #include <math.h>
>>
>> int main() {
>> double param, fractional_part, integer_part;
>> param = 1.1;
>> fractional_part = modf(param, &integer_part);
>> printf("%f = %f + %f \n", param, integer_part, fractional_part);
>> return 0;
>> }
>> ~~~
>>
>> Compile and run:
>> ~~~
>> $ gcc -Wall test.c -o test
>> $ ./test
>> 1.100000 = 1.000000 + 0.100000
>> ~~~
>>
>> E-mail: [email protected]
>> WWW: www.wouterbeek.com
>> Tel.: 0647674624
>> -------------- next part --------------
>> HTML attachment scrubbed and removed
>> _______________________________________________
>> SWI-Prolog mailing list
>> [email protected]
>> https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog
>>
> -------------- next part --------------
> HTML attachment scrubbed and removed
> _______________________________________________
> SWI-Prolog mailing list
> [email protected]
> https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog
>
_______________________________________________
SWI-Prolog mailing list
[email protected]
https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog