AutoHotkey Community

It is currently May 27th, 2012, 10:44 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: February 14th, 2008, 1:36 am 
Offline

Joined: July 15th, 2007, 1:43 am
Posts: 1320
I have recently been unable to connect to the internet, so I have been extremely bored and I decided to make functions. One of which is Up(). It is basically like floor() but instead of rounding down, it rounds up. Code:

Code:
Up(x) {
   If (InStr(x, ".")) { ; Is Decimal ; i.e. 56.000000
      StringGetPos, Pos, x, .
      If (SubStr(x, Pos + 2, StrLen(x - Pos)) > 0) { ; Decimal Part is greater than 0.
         Return floor(x) + 1 ; Return floor value of x plus 1 (x rounded up to the next whole)
      }
      Else ; Is Whole w/ decimal but decimal part is equal to nothing. ; i.e. 56.000000
         Return floor(x)
   }
   Else ;  Is Whole ; i.e. 56
      Return floor(x)
}


Example:

Code:
MsgBox % Up("1.5")


Comment :shock:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 14th, 2008, 2:14 am 
Offline
User avatar

Joined: October 7th, 2006, 8:45 am
Posts: 3330
Location: Simi Valley, CA
isn't that the same as
Code:
Up(nm)
{
   return % (Floor(nm) + (Floor(nm) < nm))
}
???

_________________
Ternary (a ? b : c) guide     TSV Table Manipulation Library
Post code inside [code][/code] tags!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 14th, 2008, 2:19 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7503
Location: Australia
Here's my version:
Code:
Up(x) {
    return Ceil(x)
}
:lol:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 14th, 2008, 2:21 am 
Offline

Joined: July 15th, 2007, 1:43 am
Posts: 1320
Oh, I wasn't aware that there was a function already built-in for this :)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 14th, 2008, 2:30 am 
Offline
User avatar

Joined: October 7th, 2006, 8:45 am
Posts: 3330
Location: Simi Valley, CA
lol ! I didn't even stop to think about those kinds of miscellaneous math functions, since I never use them in scripts. Usually, I either SetFormat or just let other commands coerce floats into ints (like Click X Y.

_________________
Ternary (a ? b : c) guide     TSV Table Manipulation Library
Post code inside [code][/code] tags!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 14th, 2008, 3:35 am 
Offline

Joined: June 26th, 2006, 6:14 pm
Posts: 1379
Location: USA
Ian wrote:
Oh, I wasn't aware that there was a function already built-in for this :)

On the forum Laszlo has probably posted every math function you would ever need. (for the ones not built in). But then again, you said you could not be connected. :P

_________________
Image
ʞɔпɟ əɥʇ ʇɐɥʍ


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 14th, 2008, 8:58 am 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
Whats the difference with this and Ceil()?

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 14th, 2008, 10:38 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7503
Location: Australia
I should hope there is no difference...
Ian wrote:
Oh, I wasn't aware that there was a function already built-in for this


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 14th, 2008, 2:53 pm 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
Ah, I never usually look past the first post. Shows we all have more to learn :)

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 14th, 2008, 5:11 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Floor rounds toward -∞, Ceil rounds toward +∞. Sometimes you want to round toward TO 0, or away FROM 0. These are not in AHK, so here they are as user functions:
Code:
MsgBox % floor(1.1) ", " floor(-1.1)
MsgBox % ceil(1.1)  ", " ceil(-1.1)
MsgBox % from0(1.1) ", " from0(-1.1)
MsgBox % to0(1.1)   ", " to0(-1.1)

from0(x) {
   Return x < 0 ? floor(x) : ceil(x)
}

to0(x) {
   Return x < 0 ? ceil(x) : floor(x)
}


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 19 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group