[mercury-users] Math Module
Nancy Mazur
Nancy.Mazur at cs.kuleuven.ac.be
Mon Oct 21 23:49:13 AEST 2002
* Noel Pinto <cool4life at rediffmail.com> [2002-10-21 15:42]:
> Hi,
>
> I want to write a module which would have a number which would
> be a float and then it should be rounded to the nearest number. At
> each stage it should be displayed on the screen. But I get errors
> , plz do help me solve it.
>
> :- module 'Nex_int'.
>
> :- interface.
> :- import_module io.
> :- pred main(io__state, io__state).
> :- mode main(di, uo) is det.
>
> :- implementation.
> :- import_module math, float.
> main --> { Radius = 10.5 }, print(Radius), {Radius =
> math__round(Radius)}, print(Radius).
>
> Here is the error...
> Nex_int.m:006: In `main(di, uo)':
> Nex_int.m:006: error: determinism declaration not satisfied.
> Nex_int.m:006: Declared `det', inferred `semidet'.
> Nex_int.m:010: In the return value of call to function
> `math:round/1':
> Nex_int.m:010: unification with `Radius' can fail.
>
> Whatever I try by changing its determinism, it does not solve the
> problem. Plz do help.
Mercury is a pure logic programming language, which means that the value
of variables can only be attributed once during the lifetime of a
variable. Take pure mathematics, what you write there is that
you assign value 10.5 to Radius, and then you want to verify the
equality whether or not Radius = math_round(Radius). Of course the
answer is no! (10.5 will never be equal to the rounded value of 10.5!).
You might perhaps consider reading more about the basics of logic
programming languages. In the FAQ of comp.lang.prolog you may find some
interesting references to books introducing you to logic programming, or
more specifically Prolog (which is also a logic programming language).
==> http://www.cs.kuleuven.ac.be/~remko/prolog/faq/
But anyway, the answer to your problem is:
..., { RoundedRadius = math__round(Radius) },
print(RoundedRadius), ...
> >> 2) I would like to know how do I use math__ceiling,
> >math__floor
> >> and math__truncate? It is a bit confusing as to how to apply
> >the
> >> logic for writng the code.
> >
> >You use that in the same way than in your example, instantiating
> >logic
> >variables or comparing values, like in
> >
> >math__ceiling(Num) = math__round(Num)
> if math__ceiling is used like this, then what is the need for such
> operations. Why cant it be like having some fixed number which
> would be a ceiling or the top limit rather than having it like
> this.
I don't understand your question..
Have fun,
Nancy
--------------------------------------------------------------------------
mercury-users mailing list
post: mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe: Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------
More information about the users
mailing list