Re: Fractional part of decimal value.

Carlo Capelli <[email protected]>
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <CABty9wyjq6mM10bra+38f=1_z3228NtMzpeHXftJoLkYAaX-qQ@mail.gmail.com>
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),
...

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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.