How to trunc a real?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to trunc a real?

Jon Kleiser
Probably the most stupid Lua question ever asked: What's the easiest way to cut away the fractional part of a floating point number in Lua?

/Jon
---------------------------------------------------------------------------
Jon Kleiser / ADB ekstern / USIT / University of Oslo / Norway
Mail: [hidden email] / Tel: +47-22 85 28 04 / Fax: +47-22 85 29 70
---------------------------------------------------------------------------

Reply | Threaded
Open this post in threaded view
|

RE: How to trunc a real?

Vincent Penquerc'h-3
> Probably the most stupid Lua question ever asked: What's the easiest 
> way to cut away the fractional part of a floating point number in Lua?

Since you only have one numerical type, I think the only way (besides
calling a registered routine of course) is to convert the number to text
format (via the format routine), and parse it.

-- 
Lyrian 


Reply | Threaded
Open this post in threaded view
|

RE: How to trunc a real?

Reuben Thomas-3
> Since you only have one numerical type, I think the only way (besides
> calling a registered routine of course) is to convert the number to text
> format (via the format routine), and parse it.

You can use:

function int(n)
  if n < 0 then return ceil(n)
  return floor(n)
end

(untested, but you get the idea)

-- 
http://sc3d.org/rrt/ | Si hoc legere scis nimium eruditionis habes.


Reply | Threaded
Open this post in threaded view
|

Re: How to trunc a real?

Steve Dekorte-6
In reply to this post by Jon Kleiser
Jon Kleiser wrote:
> Probably the most stupid Lua question ever asked: What's the easiest  
> way to cut away the fractional part of a floating point number in Lua? 

ceil() or floor() will do the trick.

Steve