jlandes Posted June 15, 2004 Posted June 15, 2004 I have the following two functions for inclusion in the standard library. However, these functions do not work correctly. If I do _Ceil(2), the function should return 2, but it returns 3. I know why it's happening, but I don't know exactly how _Ceil() and _Floor() are really supposed to work and if there is a standard way of accomplishing these two functions. Can someone let me know what's wrong with them and how to fix it? ;============================================================================= ; ; Function Name: _Ceil() ; Description: Returns the smallest integer greater than or equal to the ; specified value. ; Author(s): Brian Keene <brian_keene@yahoo.com> ; ;============================================================================= Func _Ceil($nValue) If (Not IsNumber($nValue)) Then SetError(1) Return 0 EndIf SetError(0) Return(Int($nValue) + 1) EndFunc ;==> _Ceil() ;============================================================================= ; ; Function Name: _Floor() ; Description: Returns the greatest integer less than or equal to the ; specified value. ; Author(s): Brian Keene <brian_keene@yahoo.com> ; ;============================================================================= Func _Floor($nValue) If (Not IsNumber($nValue)) Then SetError(1) Return 0 EndIf Return(Int($nValue)) EndFunc ;==> _Floor() Sincerely yours,Jeremy Landesjlandes@landeserve.com
Marc Posted June 15, 2004 Posted June 15, 2004 Ceil should return the next bigger/equal integer. ceil(2) = 2 ceil (2.1) = 3 ceil (2.9) = 3 ceil (-2.1) = -2 if IsFloat($nValue) then Return(Int($nValue) + 1) else return ($nValue) endif Floor is next lesser/equal integer. floor (-2.1) = -3 floor (2.1) = 2 floor (-2) = -3 if IsFloat($nValue) then if $nValue > 0 then Return(Int($nValue)) else Return(Int($nValue-1)) endif else return ($nValue) endif Code is untested, just hacked it in Any of my own codes posted on the forum are free for use by others without any restriction of any kind. (WTFPL)
Nutster Posted June 16, 2004 Posted June 16, 2004 More untested code, but I have worked with these ideas for a while. Func Ceil($value) Return -(Floor(-$Value)) EndFunc Func Floor($Value) Return Int($value) EndFunc David NuttallNuttall Computer Consulting An Aquarius born during the Age of Aquarius AutoIt allows me to re-invent the wheel so much faster. I'm off to write a wizard, a wonderful wizard of odd...
jlandes Posted June 16, 2004 Author Posted June 16, 2004 Please feel free to send some of your ideas my way. Sincerely yours,Jeremy Landesjlandes@landeserve.com
jlandes Posted June 16, 2004 Author Posted June 16, 2004 More untested code, but I have worked with these ideas for a while. Func Ceil($value) Return -(Floor(-$Value)) EndFunc Func Floor($Value) Return Int($value) EndFuncThis doesn't work. When I tried Ceil(2.1), I should have got 3 back, instead I got 2. The previous version that was posted seems to work well. Thanks. Sincerely yours,Jeremy Landesjlandes@landeserve.com
pekster Posted June 16, 2004 Posted June 16, 2004 Func Ceil($value) If $value = int($value) Then return $value return int($value) + 1 EndFunc [font="Optima"]"Standing in the rain, twisted and insane, we are holding onto nothing.Feeling every breath, holding no regrets, we're still looking out for something."[/font]Note: my projects are off-line until I can spend more time to make them compatable with syntax changes.
Lazycat Posted June 16, 2004 Posted June 16, 2004 Here is my variant Func Ceil($num) $rnd = Round($num) If $rnd - $num < 0 Then $rnd = $rnd + 1 Return ($rnd) EndFunc Func Floor($num) $rnd = Round($num) If $rnd - $num > 0 Then $rnd = $rnd - 1 Return ($rnd) EndFunc Koda homepage ([s]Outdated Koda homepage[/s]) (Bug Tracker)My Autoit script page ([s]Outdated mirror[/s])
Nutster Posted June 16, 2004 Posted June 16, 2004 (edited) This doesn't work. When I tried Ceil(2.1), I should have got 3 back, instead I got 2. The previous version that was posted seems to work well. Thanks.This fixes it: Func Ceil($value) Return -(Floor(-$value)) EndFunc ;==>Ceil Func Floor($value) If IsInt($value) Then Return $value ElseIf $value < 0 Then Return Int($value - 1) Else Return Int($value) EndIf EndFunc ;==>Floor I did not properly anticipate the way that Int() handles negative numbers. Edited June 16, 2004 by Nutster David NuttallNuttall Computer Consulting An Aquarius born during the Age of Aquarius AutoIt allows me to re-invent the wheel so much faster. I'm off to write a wizard, a wonderful wizard of odd...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now