Indigo Rose Software

Professional Software Development Tools

 
Results 1 to 11 of 11
  1. #1
    Join Date
    Dec 2003
    Location
    Raleigh, NC
    Posts
    80

    Huh? Is there something wrong with Math.Floor?

    I have a program created with AMS5 that converts hours to minutes. It also converts the hours (say a decimal fraction like 2.0125 hours) to hours and minutes. The program converts the hours into minutes by multiplying by 60. In this example, the result is 120.75 minutes. So far so good. Then, when I try to convert these 120.75 minutes into hours + minutes, I encounter a problem. I decided to strip the integer portion of this decimal number by using the Math.Floor function. So far I have the number 120.75 stored in the variable Min, which prints to the screen correctly as 120.75. The code I used is:
    WholeMin = Math.Floor(Min);
    This stores the number 120 in the variable WholeMin. Then I strip off this integer portion with the following code:
    MinFxn = Min - WholeMin;
    This stores the number 0.75 in the variable MinFxn. To convert the minutes to hours (in the variable Hr1) plus minutes (in the variable Min1), I used:
    Hr1 = WholeMin / 60; (this stores 2 in Hr1)
    and
    Min1 = MinFxn;
    The code I used to display Min1 is:
    Paragraph.SetText("Min1Para", Min1.."");
    Unfortunately, the result displaying in this paragraph object is 0.75000000000001 rather than 0.75
    Is there something wrong with Math.Floor function or is it something I have done?

  2. #2
    Corey is offline Indigo Rose Staff Alumni
    Join Date
    Aug 2002
    Posts
    9,746
    Hi. Attached is a simple example of how to do this more easily. Take a look in the Global functions area for the math. Hope that helps.
    Attached Files

  3. #3
    Join Date
    Dec 2003
    Location
    Raleigh, NC
    Posts
    80
    Hi, Corey. Thanks for taking the time and trouble to do this. It converts hrs to minutes and vice versa nicely. What I need for my program to do, however, is a little bit different. The user inputs hours elapsed as a decimal fraction. The program computes minutes and displays them as a straight decimal fraction (a simple multiplication of the hours input by 60). Now the hooker: the program is then supposed to display the time elapsed as whole hours and then a separate field for the remaining minutes. Thus, the page should look like this:
    -----------Hours: 2.0125 (user inputs elapsed time in decimal hours)
    -----------Minutes: 120.75 (this displays correctly on the screen)
    -----------Hours: 2 & Minutes: 0.75
    The number of Hours (2) displays correctly, however, to my shock, the "& Minutes" box displays 0.75000000000001 instead of 0.75!!!
    I've got to get rid of that "0000000000001" and have a neat result of 0.75. If you know of some other way to break down a decimal hour into whole hours and remaining minutes that calculates a neat result, I'd sure appreciate learning it. One happy human face (mine) to the solver of this problem!

  4. #4
    Corey is offline Indigo Rose Staff Alumni
    Join Date
    Aug 2002
    Posts
    9,746
    I just use Math.Round(Min, 2);

  5. #5
    Join Date
    Dec 2003
    Location
    Raleigh, NC
    Posts
    80
    That certainly works in this particular instance (2 decimal places), however, it was just a chance thing that this result was 2 decimal places. It could be any # of places. I have input other #'s and the program calculates them correctly; it's just this one case. My only conclusion is that this is a defect in the Lua math. I'll just leave it at that unless I hear different from someone in the forum and hope it doesn't pop up again. Thanks for your concern.

  6. #6
    Corey is offline Indigo Rose Staff Alumni
    Join Date
    Aug 2002
    Posts
    9,746
    Yeah it's definitely some kind of anomaly.

  7. #7
    Join Date
    Oct 2001
    Location
    Norway
    Posts
    939
    dthompson16,

    I have previously had trouble with the Floor function; look at this thread. At that time we didn't have the Math.Round() in AMS50.

    I'm neither a mathematician nor a computer expert, but I don't think that the floor function is causing your trouble. I believe you have
    Code:
    Min = Hours * 60			-- 120.75
    WholeMin = Math.Floor(Min);	-- 120
    MinFxn = Min - WholeMin;		-- 120.75 - 120 = 0.75000000000001
    Change the last line into:
    Code:
    MinFxn = String.ToNumber(Min.."") - WholeMin;
    That is: Convert the numeric value Min into a string value, and then convert it back to a number.

    If it helps; don't ask me WHY! There has to be some kind of anomaly. Maybe it has something to do with how Lua stores numeric values in memory. "100 years ago" I had a similar problem with another language (dBase II at an early stage?), and that was in an accounting program! 120.34 - 12.15 = 108,18999987 isn't good. I had to use the Round function to have 108.19.

  8. #8
    Join Date
    Dec 2003
    Location
    Raleigh, NC
    Posts
    80

    Thumbs up

    Csd, you saw the problem perfectly and your solution worked like a charm. Thanks so much!

  9. #9
    Join Date
    Oct 2001
    Location
    Norway
    Posts
    939
    I'm glad this strange work around was helpful.

    It is NOT the floor function causing the trouble, it is the multiplication! If you add a Math.Round() statement, you don't have to convert the number to a string and convert the string to a number:
    Code:
    Hours = 2.0125;
    Min = Hours * 60			-- = 120.75
    Min = Math.Round(Min, 7);		-- = 120.75
    WholeMin = Math.Floor(Min);		-- = 120
    MinFxn = Min - WholeMin;		-- 120.75 - 120 = 0.75
    -- Without tha Math.Round() MinFxn would equal 0.75000000000001 [ 11 x zero ]
    You can do the rounding with 2 – 7 decimal places. If you increase the number to 8, the "fun" starts:
    DecimalPlaces/Result MinFxn
    7 = 0.75
    8 = 0.90098113
    9 = 0.490915713
    10 = 0.0614189825

    I should like the BRAIN TRUST to tell me why it is like that, and why converting back and forth gives another result.

  10. #10
    Join Date
    Dec 2003
    Location
    Raleigh, NC
    Posts
    80
    Most interesting, csd. Thank you for analyzing this and getting to the root of it. I do hope also that the brain trust will let us know why this behavior occurs...

  11. #11
    Join Date
    Oct 2001
    Location
    Norway
    Posts
    939
    v5.0.3.0 fix:
    Fixed an error in the Math.Round action that would cause incorrect results to be returned when using high decimal precision.

    Still 120.75 – 120 gives 0.75000000000001 (instead of 0.75), so you have to use the Math.Round function. The number of decimal digits is unknown (in this case). What's new in v5.0.3.0: You can have a high precision (like 10).

    nMinFxn503 = Math.Round(MinFxn, 10);

    The result is now 0.75 (correct); with v5.0.20 the result was 0.0614189825 (post #9). If you use 14 as the number of decimals, the result is 0.75000000000001. Now it is understandable; thanks.

Similar Threads

  1. Dependencies: Doing it wrong but where?
    By synistics in forum AutoPlay Media Studio 5.0
    Replies: 10
    Last Post: 06-03-2004, 09:46 PM
  2. Wrong Error Code (When not reaching host)
    By Thierry_Zoller in forum Setup Factory 6.0
    Replies: 0
    Last Post: 04-21-2004, 06:53 PM
  3. what is wrong with this script??
    By Martin_SBT in forum AutoPlay Media Studio 5.0
    Replies: 5
    Last Post: 12-05-2003, 02:25 PM
  4. Does anyone know what I am doing wrong?
    By Scampula in forum AutoPlay Media Studio 5.0
    Replies: 2
    Last Post: 12-05-2003, 05:12 AM
  5. NEED HELP ON A MESSAGE AFTER A WRONG PASSWORD
    By dragon in forum AutoPlay Menu Studio 3.0
    Replies: 0
    Last Post: 02-22-2002, 12:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts