Re: Fractional part of decimal value.

Paulo Moura <[email protected]>
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <[email protected]>
On 07/08/2013, at 15:14, Wouter Beek <[email protected]> wrote:

> 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
> ~~~


I assume that you're aware of the float_integer_part/1 and float_fractional_part/1 ISO Prolog standard functions but for some reason you found them not adequate for your task?

Cheers,

Paulo


-----------------------------------------------------------------
Paulo Moura
Logtalk developer

Email: <mailto:[email protected]>
Web:   <http://logtalk.org/>
-----------------------------------------------------------------
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.