Math: Add floor() and ceiling() functions

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Math: Add floor() and ceiling() functions

Post by IdeasVacuum »

IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

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

Post 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
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

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

Post 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.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

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

Post 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.
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

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

Post 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.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

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

Post 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.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply