Type casting and truncation

24 views
Skip to first unread message

Lance Galletti

unread,
Jul 26, 2017, 4:26:51 PM7/26/17
to ats-lang-users
Hi,

I am trying to make a function that truncates numbers. For example turning 1.4523 into 1.45. It occurred to me that if I had a function that could change the type of a number to int (call it floor()) then, using the same example I would only need to do floor( 1.4523*100 ), then divide the result by 100.0. Where can I find such a function in ATS? Are there others - like say from int to string, from template type a to template type b?

Thank you!

gmhwxi

unread,
Jul 26, 2017, 5:06:35 PM7/26/17
to ats-lang-users
The floor function is in libats/libc/math:

#staload M = "libats/libc/SATS/math.sats"

val x = 1.234
val y = $M.floor(x) // y is of type 'double'

Or you can do:

#staload UN = "prelude/SATS/unsafe.sats"

val z = $UN.cast{int}(x) // z is of type 'int'

For int-to-string, please see the function iota in libats/ML/SAT/string.sats.

gmhwxi

unread,
Jul 26, 2017, 5:16:35 PM7/26/17
to ats-lang-users

>>For int-to-string,
>>please see the function iota in libats/ML/SAT/string.sats.

The function iota is in libats/ML/SATS/string.sats.

Also, the function g0string2int can be called to turn a string into an integer.

As a rule of thumb, if you target C, then you just need to find out how such
functions are handled in C. If you target JS, then try JS. There is really "native"
version of these functions in ATS.
Reply all
Reply to author
Forward
0 new messages