roundUp function?

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

roundUp function?

Roger Eller
 We have round, which will either round up or down depending on the decimal
value being > or < .5, but what if I want ANY decimal value, even .01 to
round UP to the next whole number?

Dictionary Examples:
the round of 26.2 -- yields 26
the round of 2.5 -- yields 3 (rounds up)

I want to:
get roundUP(26.2) -- yields 27

set the itemDel to "."
--------- I could just evaluate what falls after the decimal.
if item 2 of MyDecimalNum > 0 then
   add 1 to item 1 of MyDecimalNum
   put 0 into item 2 of MyDecimalNum
end if

Is there a better way?  Probably, I'm guessing...

~Roger
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: roundUp function?

Peter TB Brett


On 22/11/2016 14:15, Roger Eller wrote:
>  We have round, which will either round up or down depending on the decimal
> value being > or < .5, but what if I want ANY decimal value, even .01 to
> round UP to the next whole number?

You probably want to use "the ceiling of <number>", or "the ceil of
<number>".

Best regards,

                                      Peter

--
Dr Peter Brett <[hidden email]>
LiveCode Technical Project Manager

lcb-mode for Emacs: https://github.com/peter-b/lcb-mode

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: roundUp function?

Roger Eller
Thanks, Peter!

~Roger


On Tue, Nov 22, 2016 at 9:27 AM, Peter TB Brett <[hidden email]>
wrote:

>
>
> On 22/11/2016 14:15, Roger Eller wrote:
>
>>  We have round, which will either round up or down depending on the
>> decimal
>> value being > or < .5, but what if I want ANY decimal value, even .01 to
>> round UP to the next whole number?
>>
>
> You probably want to use "the ceiling of <number>", or "the ceil of
> <number>".
>
> Best regards,
>
>                                      Peter
>
> --
> Dr Peter Brett <[hidden email]>
> LiveCode Technical Project Manager
>
> lcb-mode for Emacs: https://github.com/peter-b/lcb-mode
>
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: roundUp function?

Mike Bonner
Kinda pointless since you can use ceiling, but you could go old school and
add .5 to the number, then round to achieve the same affect.

On Tue, Nov 22, 2016 at 7:29 AM, Roger Eller <[hidden email]>
wrote:

> Thanks, Peter!
>
> ~Roger
>
>
> On Tue, Nov 22, 2016 at 9:27 AM, Peter TB Brett <[hidden email]>
> wrote:
>
> >
> >
> > On 22/11/2016 14:15, Roger Eller wrote:
> >
> >>  We have round, which will either round up or down depending on the
> >> decimal
> >> value being > or < .5, but what if I want ANY decimal value, even .01 to
> >> round UP to the next whole number?
> >>
> >
> > You probably want to use "the ceiling of <number>", or "the ceil of
> > <number>".
> >
> > Best regards,
> >
> >                                      Peter
> >
> > --
> > Dr Peter Brett <[hidden email]>
> > LiveCode Technical Project Manager
> >
> > lcb-mode for Emacs: https://github.com/peter-b/lcb-mode
> >
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: roundUp function?

Mike Bonner
Actually, .4 I guess.

On Tue, Nov 22, 2016 at 7:38 AM, Mike Bonner <[hidden email]> wrote:

> Kinda pointless since you can use ceiling, but you could go old school and
> add .5 to the number, then round to achieve the same affect.
>
> On Tue, Nov 22, 2016 at 7:29 AM, Roger Eller <[hidden email]>
> wrote:
>
>> Thanks, Peter!
>>
>> ~Roger
>>
>>
>> On Tue, Nov 22, 2016 at 9:27 AM, Peter TB Brett <[hidden email]
>> >
>> wrote:
>>
>> >
>> >
>> > On 22/11/2016 14:15, Roger Eller wrote:
>> >
>> >>  We have round, which will either round up or down depending on the
>> >> decimal
>> >> value being > or < .5, but what if I want ANY decimal value, even .01
>> to
>> >> round UP to the next whole number?
>> >>
>> >
>> > You probably want to use "the ceiling of <number>", or "the ceil of
>> > <number>".
>> >
>> > Best regards,
>> >
>> >                                      Peter
>> >
>> > --
>> > Dr Peter Brett <[hidden email]>
>> > LiveCode Technical Project Manager
>> >
>> > lcb-mode for Emacs: https://github.com/peter-b/lcb-mode
>> >
>> _______________________________________________
>> use-livecode mailing list
>> [hidden email]
>> Please visit this url to subscribe, unsubscribe and manage your
>> subscription preferences:
>> http://lists.runrev.com/mailman/listinfo/use-livecode
>>
>
>
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: roundUp function?

Roger Eller
In reply to this post by Roger Eller
Peter,

ceil works in 8.1.1, and in the docs of 8.1.1 says "Introduced in 6.7", yet
it fails in 6.7.5 and6.7.8, nor is it in the 6.7.x dictionary.

That said, I need it in an app that is built and deployed in 6.7.5.

~Roger


On Tue, Nov 22, 2016 at 9:29 AM, Roger Eller <[hidden email]>
wrote:

> Thanks, Peter!
>
> ~Roger
>
>
> On Tue, Nov 22, 2016 at 9:27 AM, Peter TB Brett <[hidden email]>
> wrote:
>
>>
>>
>> On 22/11/2016 14:15, Roger Eller wrote:
>>
>>>  We have round, which will either round up or down depending on the
>>> decimal
>>> value being > or < .5, but what if I want ANY decimal value, even .01 to
>>> round UP to the next whole number?
>>>
>>
>> You probably want to use "the ceiling of <number>", or "the ceil of
>> <number>".
>>
>> Best regards,
>>
>>                                      Peter
>>
>> --
>> Dr Peter Brett <[hidden email]>
>> LiveCode Technical Project Manager
>>
>> lcb-mode for Emacs: https://github.com/peter-b/lcb-mode
>>
>
>
>
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: roundUp function?

Roger Eller
In reply to this post by Mike Bonner
Old school is just awesome sometimes.  :)

Thanks for that idea, Mike.

~Roger


On Tue, Nov 22, 2016 at 9:39 AM, Mike Bonner <[hidden email]> wrote:

> Actually, .4 I guess.
>
> On Tue, Nov 22, 2016 at 7:38 AM, Mike Bonner <[hidden email]> wrote:
>
> > Kinda pointless since you can use ceiling, but you could go old school
> and
> > add .5 to the number, then round to achieve the same affect.
> >
> > On Tue, Nov 22, 2016 at 7:29 AM, Roger Eller <
> [hidden email]>
> > wrote:
> >
> >> Thanks, Peter!
> >>
> >> ~Roger
> >>
> >>
> >> On Tue, Nov 22, 2016 at 9:27 AM, Peter TB Brett <
> [hidden email]
> >> >
> >> wrote:
> >>
> >> >
> >> >
> >> > On 22/11/2016 14:15, Roger Eller wrote:
> >> >
> >> >>  We have round, which will either round up or down depending on the
> >> >> decimal
> >> >> value being > or < .5, but what if I want ANY decimal value, even .01
> >> to
> >> >> round UP to the next whole number?
> >> >>
> >> >
> >> > You probably want to use "the ceiling of <number>", or "the ceil of
> >> > <number>".
> >> >
> >> > Best regards,
> >> >
> >> >                                      Peter
> >> >
> >> > --
> >> > Dr Peter Brett <[hidden email]>
> >> > LiveCode Technical Project Manager
> >> >
> >> > lcb-mode for Emacs: https://github.com/peter-b/lcb-mode
> >> >
> >> _______________________________________________
> >> use-livecode mailing list
> >> [hidden email]
> >> Please visit this url to subscribe, unsubscribe and manage your
> >> subscription preferences:
> >> http://lists.runrev.com/mailman/listinfo/use-livecode
> >>
> >
> >
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: roundUp function?

Peter TB Brett
In reply to this post by Roger Eller


On 22/11/2016 14:40, Roger Eller wrote:

> ceil works in 8.1.1, and in the docs of 8.1.1 says "Introduced in 6.7", yet
> it fails in 6.7.5 and6.7.8, nor is it in the 6.7.x dictionary.
>
> That said, I need it in an app that is built and deployed in 6.7.5.

Hi Roger,

That appears to be a documentation error.  Mark Wieder introduced the
function in 7.1.0 [1].

                                            Peter

[1]
https://github.com/livecode/livecode/commit/d919382bcd303b631844cd56c4dd3157b2aa8180

--
Dr Peter Brett <[hidden email]>
LiveCode Technical Project Manager

lcb-mode for Emacs: https://github.com/peter-b/lcb-mode

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: roundUp function?

Roger Eller
Good to know.  Thanks again.


On Tue, Nov 22, 2016 at 9:44 AM, Peter TB Brett <[hidden email]>
wrote:

>
>
> On 22/11/2016 14:40, Roger Eller wrote:
>
> ceil works in 8.1.1, and in the docs of 8.1.1 says "Introduced in 6.7", yet
>> it fails in 6.7.5 and6.7.8, nor is it in the 6.7.x dictionary.
>>
>> That said, I need it in an app that is built and deployed in 6.7.5.
>>
>
> Hi Roger,
>
> That appears to be a documentation error.  Mark Wieder introduced the
> function in 7.1.0 [1].
>
>                                            Peter
>
> [1] https://github.com/livecode/livecode/commit/d919382bcd303b63
> 1844cd56c4dd3157b2aa8180
>
> --
> Dr Peter Brett <[hidden email]>
> LiveCode Technical Project Manager
>
> lcb-mode for Emacs: https://github.com/peter-b/lcb-mode
>
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: roundUp function?

Peter TB Brett


On 22/11/2016 14:48, Roger Eller wrote:
> Good to know.  Thanks again.

And I've now submitted a pull request to fix that.  Thank you for
pointing it out!

https://github.com/livecode/livecode/pull/4911

                                            Peter

--
Dr Peter Brett <[hidden email]>
LiveCode Technical Project Manager

lcb-mode for Emacs: https://github.com/peter-b/lcb-mode

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: roundUp function?

Quentin Long
In reply to this post by Roger Eller
sez Roger Eller <[hidden email]>:
> We have round, which will either round up or down depending on the decimal
> value being > or < .5, but what if I want ANY decimal value, even .01 to
> round UP to the next whole number?
The best solution is the Ceiling function. Since that's not an option for the version of LiveCode you're working with, you gotta roll your own.

The proposed "return int (DerNumber +.5)" solution won't do, because any number whose decimal part is *less than* .5 will round down to the integer part. Instead, what you want is something like this:

if DerNumber = int (DerNumber) then -- DerNumber doesn't *have* a decimal part
  return DerNumber
else -- DerNumber *does* have a decimal part, hence rounds up
  return int (DerNumber) + 1
end if

That works fine for positive numbers and zero. Negative numbers, not so much. So, a tweak to handle negative numbers:

if DerNumber = int (DerNumber) then -- DerNumber doesn't *have* a decimal part
  return DerNumber
else -- DerNumber *does* have a decimal part, hence rounds up
  return int (DerNumber) + (DerNumber / abs (DerNumber))
end if

The expression "DerNumber / abs (DerNumber)" yields a value of +1 when DerNumber is a positive number, and -1 when DerNumber is negative. Alas, it runs into problems when DerNumber is zero. So, tweaking again:

function CeilingOf DerNumber
  if DerNumber = 0 then
    return 0
  else
    if DerNumber = int (DerNumber) then -- DerNumber doesn't *have* a decimal part
      return DerNumber
    else -- DerNumber *does* have a decimal part, hence rounds up
      return 1 + int (DerNumber)
    end if
  end if
end CeilingOf

Please don't feed a non-number to this function. It won't like that.
 
"Bewitched" + "Charlie's Angels" - Charlie = "At Arm's Length"
   
Read the webcomic at [ http://www.atarmslength.net ]!
   
If you like "At Arm's Length", support it at [ http://www.patreon.com/DarkwingDude ].

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode