Basic math

ieri

New member
I'm trying to hack my first script together, but I need it to divide a number by 2, rounding down to the nearest integer. I realize this is pretty basic, but I've figured out most my scripting by picking apart other scripts for the individual commands I need. I just can't find an example of this particular function.

Thanks,

ieri
 

Winterbay

Active member
As long as the number you store is an integer any division you do will be rounded down to the nearest integer (or rather the decimals will be scrapped and ignored):
Code:
> ash int test = 5/3;

Returned: 1

> ash int test = 5.0/3;

Returned: 1

> ash float test = 5.0/3;

Returned: 1.6666666

> ash int test = 5.0/3.0;

Returned: 1

Edit:
If the number you store is a float you can still get the same effect given that the two numbers you divide are both integers:
Code:
> ash float test = 5/3;

Returned: 1.0
 

Theraze

Active member
Yes... if you actually want to round numbers, you can use ceil (up) and floor (down). But when doing int division, it doesn't care about precision... it's already lost before it begins.
Code:
[COLOR=#808000]> ash float test = 5/3

[/COLOR]Returned: 1.0

[COLOR=olive]> ash float test = ceil(5/3)[/COLOR]

Returned: 1.0

[COLOR=olive]> ash float test = ceil(5.0/3)[/COLOR]

Returned: 2.0 
 
[COLOR=#808000]> ash float test = floor(5.0/3)

[/COLOR]Returned: 1.0
 
Top