HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Rounding Values

10-03-2005, 07:38 PM#1
Tim.
Alright I have a number 0 - 80. I want to calculate, with the least possible functions, what value the number is closest to. (1, 2, 3, 4, 5, 6, 7, or 8) I could do a long series of If/Then/Else however I'm sure theres a way with a lot less effort.
10-03-2005, 08:56 PM#2
Anitarf
Code:
function R2I2 takes real r returns integer
    local integer i = R2I(r)
    local real sig = RSignBJ(r)
    if (r-i)>(i+sig-r) then
        return R2I(i+(sig+1)/2)
    else
        return R2I(i+(sig-1)/2)
    endif
endfunction

Probably not the most optimized function in existence, but oh well... I really need sleep. It's tested, though, so it has to work. Negative and positive numbers, both. I'm really curious what kind of function Vex would make... Maybe just a bunch of ifs, ifs are fast, probably faster than all the math crap I do...
10-03-2005, 09:55 PM#3
Tim.
Hah, I did this:

Code:
If (All Conditions are True) then do (Then Actions) else do (Else Actions)
    If - Conditions
        (Tester - (Real((Integer(Tester))))) Greater than 0.50
    Then - Actions
        Set Tester = ((Real((Integer(Tester)))) + 1.00)
    Else - Actions
        Set Tester = (Real((Integer(Tester))))

Both work fine, this has to run every .10 seconds so I'll do some tests and see which runs faster.
10-03-2005, 11:14 PM#4
EdwardSwolenToe
Quote:
function R2I2 takes real r returns integer
local integer i = R2I(r)
local real sig = RSignBJ(r)
if (r-i)>(i+sig-r) then
return R2I(i+(sig+1)/2)
else
return R2I(i+(sig-1)/2)
endif
endfunction

Are you sure you tested that function? Because you didnt return a value at the line before endfunction. Therefore a crash.
10-04-2005, 09:49 AM#5
Anitarf
A crash? Ingame, or when saving the map?

Well, I can tell you, neither. I tested it, allright, it's a c&p from world editor. If there's a return both under then and else actions, then the editor apparently concludes that the function will return a value in any event and doesn't need a return at the end.

Tim, your if won't work for negative numbers, but you don't need them anyway. This makes your function simpler and probably faster, although you do one more function call (all those conversions from real to integer... and there's also the way the editor converts GUI to JASS to be considered), but your math is simpler...

Edit: oh, you run this only once every 0.1 seconds? Unless you run it more times, don't bother with optimization.
10-04-2005, 08:57 PM#6
Tim.
Alright sounds good; thanks as usual Anitarf.