HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Rounding

02-27-2009, 01:47 AM#1
Blacktastic
Is there anyway to round a real to the tenth decimal place? Or a way to simulate this?

Example: Turn 2.763 into 2.8? I need to turn it into a string afterwards but that is easily accomplished with R2S. I'd convert it to an Integer but I'd lose the tenth place.
02-27-2009, 01:54 AM#2
midiway
Collapse JASS:
native R2SW takes real r, integer width, integer precision returns string

Don't know what precision parameter means, but I'm sure this is the right way

EDIT: try with those parameters, if it doesn't work I have another solution
02-27-2009, 01:56 AM#3
akolyt0r
you could try this: I2R(R2I(X*10+0.5))/10 ...dunno but this seems pretty messy..

EDIT: fixed minor error and ..it will return 2.800 and not 2.8 :(
02-27-2009, 02:10 AM#4
Blacktastic
Presicion is what decimal place... I have no idea how I'd fill that in though.

And width... the hell...

Time to go play with it a bit. Be back in a few sir.


Edit: I have 0 idea what width does. Precision is how many decimal places out it rounds to. Strangely enough, it will ONLY round the tenths spot. It doesn't round anything farther.
02-27-2009, 02:23 AM#5
Blackroot
Why would you want to round to an Nths place? That doesnt' make much since!

Anyways; wc3 has a precision of 3 decimal places with reals so I would suggest multiplying by 1000 then splitting the 3 decimals into strings using substr, cast them to integers check if the lowest decimal is > 5 and if it is increase Ndecimal+1 by 1 until you get to the rounding point.

Needlessly complicated operation though, why would you want it?
02-27-2009, 02:26 AM#6
Blacktastic
I am working on a Casting function that can take any spell casted, singe/aoe/instant, and all in 1 function calculate damage, create a castbar, and call a function that corresponds to the spell. I simply needed this as a "tracker" to show the player how time the cast had left. Etc.

Cure (3.0) > Cure (2.9) > Cure (2.8)


Still unsure if I want to add knockback to casting upon being dealt damage, but this is EXACTLY what I needed. Thank you for that R2SW function sir.
02-27-2009, 02:46 AM#7
Blackroot
Oh. Count down using two integers and make them look like a psuedo real.

[R2SW] takes awhile. Not sure if mine's any faster though.

For example:

Expand JASS:

I haven't tested that; but the logic is there. The problem becomes casting a real to an integer; personally I'd ignore rounding and set Tick2 = Tenths digit, ignoring the rest. (Using string casting.) If that's even neccesary.
02-27-2009, 03:13 AM#8
Blacktastic
Well considering this system will be implemented for all casting, it will probably be called around 30-40 times a second so yes, speed it pretty important. I'll look into your method.
02-27-2009, 04:16 AM#9
ShadowWolf
You guys are overcomplicating this. Multiply the number by 10, round it to an integer, then divide by 10.

((int(x*10))/10)

Unless wc3 truncates instead of rounding, which would be pretty lame.
02-27-2009, 04:20 AM#10
Blackroot
Quote:
Originally Posted by ShadowWolf
You guys are overcomplicating this. Multiply the number by 10, round it to an integer, then divide by 10.

((int(x*10))/10)

Unless wc3 truncates instead of rounding, which would be pretty lame.

[edit]

Nope; it was bad coding truncating it. That method works, just very strange casting:

Expand JASS:

Returns 1.9

Someone should benchmark; this should certainly be faster then my method; but who knows. Also; R2SW might actually be faster?
02-27-2009, 04:55 AM#11
Blacktastic
<Deaod> just tested, R2SW is about as fast as I2S()

Take it as you will, but as to whether the method that was just suggested is faster or not, I do not know.
02-27-2009, 05:13 AM#12
Blackroot
Quote:
Originally Posted by Blacktastic
<Deaod> just tested, R2SW is about as fast as I2S()

Take it as you will, but as to whether the method that was just suggested is faster or not, I do not know.

If it's as fast as I2S() then it's certainly faster then my method.
02-27-2009, 05:36 AM#13
ShadowWolf
in that case...

if [(x*10 - int(x*10)) > .5] then [(x*100 + 1)/100]
else use the truncating method ((int(x*10))/10)
02-27-2009, 05:39 AM#14
Blacktastic
Yes but is'nt calling multiple I2R and R2I and I2S going to be slower than R2SW anyway?
02-27-2009, 05:44 AM#15
Blackroot
Yes. R2SW sounds like the fastest method.