Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » zMUD General Discussion
ew1075
Newbie


Joined: 21 Sep 2007
Posts: 4

PostPosted: Mon Sep 24, 2007 9:08 am   

#MATH command rounding decimals.
 
Here is a simple version of what I have set up:

#MATH answer 5/2

This should set the variable @answer equal to 2.5. Instead it removes the decimal and rounds down, leaving the variable equaling 2 instead of 2.5.

Is there any other command, or any functions i can use, that will keep the decimal on the number? I know how to use the %format function to display
the decimals, but the problem is my variable never has a decimal because the #MATH command is automatically rounding down.

Thanks in advance for the help!
Reply with quote
eclpmb
Novice


Joined: 29 Feb 2004
Posts: 36
Location: United Kingdom

PostPosted: Mon Sep 24, 2007 9:55 am   
 
Code:
#math answer 5.0/2


will put 2.5 in answer (tested with zMud 7.21)

Make sure your maths starts with floating point and it should stay floating point. Try adding 0.0 to a variable, and so on.
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Mon Sep 24, 2007 11:23 am   
 
You can also use the %float function to make a number a float.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Guinn
Wizard


Joined: 03 Mar 2001
Posts: 1127
Location: London

PostPosted: Tue Sep 25, 2007 3:12 am   
 
It's worth adding that I think 5/2 producing an integer result is standard [correct] in a number of languages. As eclpmb mentioned, using 5.0/2 tells zMud that you're looking for a Float rather than an Int, or using %float works too. But it's not uncommon that 5/2 = 2 really.
_________________
CMUD Pro, Windows Vista x64
Core2 Q6600, 4GB RAM, GeForce 8800GT
Because you need it for text... ;)
Reply with quote
jtown84
Novice


Joined: 09 Oct 2007
Posts: 36

PostPosted: Tue Oct 09, 2007 8:29 am   
 
Quote:

#al {calc} {#show %ansi( high, green)%1 %ansi( high, white)~= %float( %eval( %1))}


Thats what i have, and say i do calc 5/2 it now = 2.0 :(
_________________
Aardwolf
Reply with quote
eclpmb
Novice


Joined: 29 Feb 2004
Posts: 36
Location: United Kingdom

PostPosted: Tue Oct 09, 2007 10:12 am   
 
That's because you're doing the calculation as integer and turning the result into a float.

calc 5/2 does %float (5/2) - it works out the value in the brackets and then turns it into a float. You can't just wrap the expression in %float you need %float(5)/2
Reply with quote
Dharkael
Enchanter


Joined: 05 Mar 2003
Posts: 593
Location: Canada

PostPosted: Tue Oct 09, 2007 10:21 am   
 
#al {calc} {#show %ansi( high, green)%1 %ansi( high, white)~= %eval( %subregex( "%1", "((?<!\.)\b[0-9]+(?!\.([0-9]+\b)?|\.[0-9]+\b))", "%float(%%1)"))}
_________________
-Dharkael-
"No matter how subtle the wizard, a knife between the shoulder blades will seriously cramp his style."
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » zMUD General Discussion All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net