Integers and reals

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Alistair
Posts: 33
Joined: Mon Jun 23, 2008 6:45 am

Integers and reals

Post by Alistair » Mon Oct 13, 2008 4:04 am

I have a function that returns the integer component of a real; e.g. passing a variable containing 5.2 should return 5. I want to use the value returned as a key for retrieving an element from an array. The function appears to extract the integer correctly but when the value is returned it appears as a REAL. For example, the value 5 is returned as 5.0. This then fails to locate the correct array element. As a quick fix I tried to ROUND the value in the variable but the result was still a REAL. Is this what I should be expecting? If so, how does one use a calculated value as a key for the purpose of retrieving an element from an array?

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Post by Janschenkel » Mon Oct 13, 2008 6:00 am

Hi Alistair,

Here's another method to get just the integer part of a floating point number.

Code: Select all

on mouseUp
   repeat while it is not a number
      ask "What number?"
      if it is empty then exit mouseUp
   end repeat
   set the itemdelimiter to "."
   answer item 1 of it
end mouseUp
Just set the 'itemDelimiter' local property to a period, and return the first item.

Hope this helped,

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Post by Janschenkel » Mon Oct 13, 2008 6:07 am

Come to think of it, why not use the built-in 'trunc' function instead of building your own?

Oh, and in the interest of giving you multiple solutions - also look at the 'numberFormat' local property. This will help determine the number of decimals when converting a number to a string.

Code: Select all

function asInteger pNumber
  set the numberFormat to "9" -- no decimals
  return round(pNumber) & ""
end asInteger
The important trick here is to append an empty string to your rounded number, to coerce Revolution into converting the rounded number to a string with the current numberFormat.

Hope this helped,

Jan Schenkel.[/i]
Quartam Reports & PDF Library for LiveCode
www.quartam.com

Alistair
Posts: 33
Joined: Mon Jun 23, 2008 6:45 am

Post by Alistair » Tue Oct 14, 2008 8:29 am

Hi Jan

Appending the empty string was the key piece of information that I was missing. Thanks for pointing that out. My script is now retrieving data from the array.

I have also changed to using "trunc". The project I am working on was originally developed by someone else in Hypercard so I have been working with existing code.

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Post by mwieder » Wed Oct 15, 2008 1:53 am

trunc works fine most of the time, but you may want to continue appending an empty string to the result anyway to account for some weird rounding errors... I've now taken to doing this:

function truncate pValue
return trunc(pValue) & ""
end truncate

Post Reply