Fractional part of decimal value.
Wouter Beek <[email protected]>
| Newsgroups | gmane.comp.ai.prolog.swi |
|---|---|
| Message-ID | <CAE1un7OKFHOnxWOt81-ULeBd0MaCdCxPRt6-WcHKBXqOBZAFJQ@mail.gmail.com> |
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