Re: Fractional part of decimal value.

Wouter Beek <[email protected]>
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <CAE1un7MRg4jhUZiNnGX2gbazCpySZr8+a3NKj0xs4-jKYqVaAQ@mail.gmail.com>
Hi Paulo,

Thanks for the response! The functions that you mention have the same
outcome:
~~~
?- X is float_fractional_part(1.1).
X = 0.10000000000000009.
~~~
Even:
~~~
?- X is 1.1 - 1.
X = 0.10000000000000009.
~~~
I understand that most decimal fractions cannot be represented by binary
fractions, so there is probably a good reason to have the '9' appear at the
end.

My question is whether this can be circumvented for my simple/naive use.
(Preferably a solution that does not involve a hack such as using
format-to-codes and then number_codes/2 :-P.)

---
Cheers!,
Wouter.

E-mail: [email protected]
WWW: www.wouterbeek.com
Tel.: 0647674624


On Wed, Aug 7, 2013 at 4:21 PM, Paulo Moura <[email protected]> wrote:

>
> 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/>
> -----------------------------------------------------------------
>
>
>
>
> _______________________________________________
> 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.