Page 1 of 1

Math: Add floor() and ceiling() functions

Posted: Mon Dec 20, 2010 2:56 am
by IdeasVacuum

Re: Math: Add floor() and ceiling() functions

Posted: Mon Dec 20, 2010 6:19 am
by Demivec
Isn't this already implemented?

Code: Select all

Debug Round(f.f, #PB_Round_Up)
Debug Round(f.f, #PB_Round_Down)
or if you prefer:

Code: Select all

Macro floor(x)
  Round(x, #PB_Round_Down)
EndMacro

Macro ceiling(x)
  Round(x, #PB_Round_Up)
EndMacro

For i = 1 To 40
  f.f = (i - 20) / 4
  Debug "i: " + StrF(f) + ", up:" + Str(ceiling(f)) + ", down:" + Str(floor(f))
Next

Re: Math: Add floor() and ceiling() functions

Posted: Mon Dec 20, 2010 3:38 pm
by IdeasVacuum
It does not seem to have the precision required when using doubles. I have a work-around using strings and often the value is going to end-up being a string anyway.

Re: Math: Add floor() and ceiling() functions

Posted: Mon Dec 20, 2010 3:49 pm
by Little John
IdeasVacuum wrote:It does not seem to have the precision required when using doubles.
Can you please post some code that demonstrates the problem?
I'm using macros like the ones Demivec posted, and never encountered any problems.

Re: Math: Add floor() and ceiling() functions

Posted: Mon Dec 20, 2010 5:22 pm
by Demivec
IdeasVacuum wrote:It does not seem to have the precision required when using doubles. I have a work-around using strings and often the value is going to end-up being a string anyway.
Do you mean that the value of the number hovers around the integer value, sometimes higher and sometimes lower, so that when you round it up or down it goes the wrong way?

If this is the case then perhaps using some form of fixed-decimal may be what you need.

Re: Math: Add floor() and ceiling() functions

Posted: Tue Dec 21, 2010 12:17 am
by IdeasVacuum
Yes, I lost patience with it - I was at that time converting a simple, short piece of C++ code. However, the strings method is an altogether better solution for output.