Suneido

Integrated Application Platform

  • Home
  • Learning
    • Suneido Manual
    • Beginners
      • Inside Suneido
      • The Suneido Programming Language
      • The Suneido Database
      • Installing Suneido
      • Building Suneido
      • IDE Go To Tour
      • Upgrading To A New Release
    • Advanced
      • Canvas Control
      • DrawControl Part 1
      • DrawControl Part 2
      • DrawControl Part 3
      • SAX Like XML Processing
      • A Plug-In Architecture
      • A Simple Wizard Framework
      • An HTML Include Facility
      • An RSS 2 Feed Creator
      • MIME Generation
      • A New Add-on Facility
      • Workspace Improvement Hack
    • Mockito for Suneido
    • The Suneido Task Scheduler
    • Contributing To Suneido
    • Contributor Assignment of Copyright
    • Language Translation
    • Future Directions
    • Interview With Andrew Mckinlay
  • Forum
    • Announcements
    • Internals & Enhancements
    • Cookbook
    • General
  • FAQ
  • Screenshots
  • Downloads
  • Links

RoundUp problems

Integrated Application Platform › Forums › General › RoundUp problems

  • This topic has 5 replies, 2 voices, and was last updated 9 years, 10 months ago by ajith.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • May 28, 2013 at 4:35 pm #754
    ajith
    Participant

    Hi,
    I couldn’t find a RoundUp method for numbers.
    I tried this

    RoundUp(digits)
    {
    factor = 10.Pow(digits.Int())
    return (this* factor).Ceiling() / factor
    }

    It works fine for positive numbers. But for negative number, there is some problem:

    9.0120456789.RoundUp(4) gives 9.0121
    -9.0120456789.RoundUp(4) gives -9.0121, when it should be -9.0120

    Trying the steps from WorkSpace, I found that
    ((-9.0120456789 * 10.Pow(4)).Ceiling() )/ 10.Pow(4) gives -9.012, the correct answer.

    However, -90120.456789.Ceiling() gives -90121

    Are you able to recreate the problem? If so, what is the reason?

    BTW, suneido had collapsed when I tried putting Print(digit) inside the Round method before I tried the code. Error in Errohandler had shown value stack overflow

    ajith

    May 28, 2013 at 10:29 pm #985
    amckinlay
    Keymaster

    It’s because Suneido is interpreting it as:

    -(90120.456789.Ceiling())

    whereas what you want is:

    (-90120.456789).Ceiling()

    try using a variable:

    x = -90120.456789
    x.Ceiling()

    same problem with your RoundUp example

    May 28, 2013 at 10:48 pm #986
    amckinlay
    Keymaster

    I think the definition of RoundDown was wrong. I changed it to be like your RoundUp except with Floor instead of Ceiling. I also improved Floor and Ceiling slightly.

    Here’s what I now have in stdlib:

    	Ceiling()
    {
    i = .Int()
    return this > i ? i + 1 : i
    }
    Floor()
    {
    i = .Int()
    return this < i ? i - 1 : i
    }
    Round(digits)
    {
    factor = .Sign() * 10.Pow(digits.Int())
    return (this * factor + .5).Int() / factor
    }
    RoundDown(digits)
    {
    factor = 10.Pow(digits.Int())
    return (this * factor).Floor() / factor
    }
    RoundUp(digits)
    {
    factor = 10.Pow(digits.Int())
    return (this * factor).Ceiling() / factor
    }
    May 29, 2013 at 3:09 pm #987
    ajith
    Participant

    It’s because Suneido is interpreting it as:

    -(90120.456789.Ceiling())

    whereas what you want is:

    (-90120.456789).Ceiling()

    1) Is this interpretation right? I mean shouldn’t Suneido interpret it a negative number and then apply the method?
    2) Does this mean that when there is a possibility of a negative number being acted upon by a method, we should always use the number inside parenthesis or supply it as variable?
    3) Your explanation works for the results obtained from WorkSpace. When a Print((this * factor).Ceiling()) is used inside the RoundUp method, it gives a positive number ignoring the sign of the supplied number. When a Print(this) is used, it can be seen that this is returned as a positive number, ignoring the sign. Is this behaviour expected?
    4) When I use a Print(digit) statement inside the Round() method, Suneido crashes showing value stack overflow error.
    ajith

    May 29, 2013 at 3:30 pm #988
    amckinlay
    Keymaster

    1) I think the interpretation is correct, or at least not wrong. For example, if you had -x.Abs() I think you would want it to do .Abs() first. The question is, for -123.Fn() is the minus sign part of the number, or is it an operator as it is in -x.Fn() Currently it is treated as an operator.

    2) In general, if the precedence of operations is unclear, then it’s a good idea to use parenthesis. Even if it is only for the benefit of making the code clearer when read later.

    3) I am not sure if I understand this question. Inside Numbers methods, ‘this’ will be the original number.

    4) The stack overflow is likely because the Print code is calling the method containing the Print, so you get infinite recursion.

    May 29, 2013 at 7:20 pm #989
    ajith
    Participant

    3) I am not sure if I understand this question. Inside Numbers methods, ‘this’ will be the original number.

    Yes, it should be.
    I typed Print((this * factor).Ceiling())inside the RoundUp() method and ran -9.0123456.RoundUp(4) from WorkSpace. The Print statement printed out 90124 and the RoundUp method gave -9.0124.
    Then I tried putting Print(this) in place of the first Print((this * factor).Ceiling()). Again, I ran -9.0123456.RoundUp(4) from WorkSpace . Now, the Print statement printed out 9.0123456 followed by the RoundUp method’s result -9.0124.

    So, i thought that “this” somehow was ignoring the negative sign of the original number. I thought it was an error. Now, I know that it is solved if the number is placed inside parenthesis along with its sign.

    Thanks for the explanations
    ajith

  • Author
    Posts
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.
Log In

Search Forums

Log In
Welcome to the new Suneido Web Site and Forum.
Register
Lost Password
users of the previous PHPBB Forum should be able to log in with their previous username and password.

Recent Topics

  • Alpha copy of gSuneido to play with
  • how to start jsuneido ?
  • Problem Starting Suneido…
  • Dialog not showing buttons
  • New link for Suneidojo

Contact Us | Legal Statement | Privacy Statement | SiteMap

Copyright © 2023 Axon® Development Corporation. All Rights Reserved. - Open Source Integrated Database and Programming Language