|
Jorville Wanderer
Joined: 30 Apr 2006 Posts: 58 Location: Brookhaven, MS
|
Posted: Wed Oct 07, 2009 6:07 am
decimals and math |
ok, im sure ive missed something somewhere, ive tried every combination of %float and %int i can think of and i must not be putting it in the right place, im trying to get it to display the number preferabl;y rounded but i also want to know how to show the decimal form too, here is what i got:
Code: |
#MATH testvar2 (@testvar/100)
#PRINT @testvar2
|
testvar is 65, so 65/100 should be 0.65, instead it gives me 0
thanks for the help, i know its something simple |
|
_________________ Jorville
Player on SWMUD |
|
|
|
Fidel Beginner
Joined: 07 Oct 2009 Posts: 19 Location: Poland
|
Posted: Wed Oct 07, 2009 9:12 am |
Hi,
@testvar is integer so before / You have to cast it with %float()
Code: |
#MATH testvar2 (%float(@testvar)/100)
#PRINT @testvar2
|
Pawel |
|
|
|
Zugg MASTER
Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Wed Oct 07, 2009 4:50 pm |
If you just use integers, then CMUD defaults to integer math. To use floating point math, you can use %float as Fidel mentioned, or you can just make one of your numbers a floating point value, like this:
Code: |
testvar2 = (@testvar/100.0)
#PRINT @testvar2 |
|
|
|
|
chris-74269 Magician
Joined: 23 Nov 2004 Posts: 364
|
Posted: Thu Oct 08, 2009 5:25 am |
Is there a good reason for the math to be carried out like this? I'd imagine scripts would be faster if there weren't tons of %float() commands to make the math work normally. A simple function to imitate integer math would make much more sense and alleviate this question being asked 50 times.
|
|
|
|
Zugg MASTER
Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Thu Oct 08, 2009 4:22 pm |
Chris, I'm not sure what you are asking. Integer math is much much faster than floating point and most MUD scripts do not need floating point, so that is why CMUD defaults to Integer math. Adding the ".0" decimal to force floating point is easy and does not slow down any parsing. And even the %float function is very fast as it's handled internally by the compiler (just changes the data type of the current expression on the stack).
So there isn't any performance issue here. And the current default of Integer Math has been the same as in zMUD for the last 12 years and hasn't caused any support issues.
The mistake would be for CMUD to default to Floating point which would then slow down all scripts for no good reason. |
|
|
|
|
|