View previous topic :: View next topic 
Author 
Message 
Slysven
Joined: 10 Apr 2011 Posts: 369 Location: As "Jomin al'Bara" in WoTMUD or Wiltshire, UK

Posted: Mon Oct 10, 2011 9:48 am Post subject: Noninteger maths 


TinTin++ #MATH command is advertised as providing integer mathematics, but I was recently doing something (with a TICK regenerating procedure to work out when the MUD's internal clock changed hours) and I found that there is some limited noninteger maths availible (I guess using 'C' native variable promotion):
Code:  #MATH result 1/3; #SHOWME $result
0
#MATH result 1.0/3; #SHOWME $result
0.3
#MATH result 1.00/3; #SHOWME $result
0.33
#MATH result 1.000/3; #SHOWME $result
0.333
#MATH result 1.0000/3; #SHOWME $result
0.3333
#MATH result 1.00000/3; #SHOWME $result
0.33333
#MATH result 1.000000/3; #SHOWME $result
0.333333
#MATH result 1.0000000/3; #SHOWME $result
0.3333330
#VARIABLE x 1; #MATH result ($x + 0.000)/3; #SHOWME $result
0.333 
Basically by added a zero with N places of decimal we also get a result to N decimal places, where N <= 6. Is this an undocumented software feature or can we accept it as designed? I would like #MATH functions to provide INT, CEILING and FLOOR integer results given a (possibly) noninteger arguments (would 'i', 'c', and 'f' work alongside the existing 'd' for random dice roll for example) is this a 'feasable idea'?
NB I recognise it is possible to obtain an INT result by using a #REGEXP {%d.%d} on a variable and then just taking the &1 result. 

Back to top 


Scandum Site Admin
Joined: 03 Dec 2004 Posts: 3818

Posted: Mon Oct 10, 2011 5:10 pm Post subject: 


#math supports double precision math, not sure why the precision is only 6 digits instead of 16, I guess it'll suffice for most people.
Probably best to write a floor and ceiling functions yourself given few people would use it.
#format supports %d for integers, and %m for tintin math strings.
I probably need to add %f to #format to handle floating point digits a little better as I don't think you can currently easily downconvert 2.234 to 2.23. 

Back to top 


Slysven
Joined: 10 Apr 2011 Posts: 369 Location: As "Jomin al'Bara" in WoTMUD or Wiltshire, UK

Posted: Tue Oct 11, 2011 11:55 am Post subject: 


Um, checking out the maths stuff and division by zero is not caught as an error!
Code:  #MATH result 10/2; #SHOWME $result
5
#MATH result 10/1; #SHOWME $result
10
#MATH result 10/1; #SHOWME $result
10
#MATH result 10/0.5; #SHOWME $result
20.0
#MATH result 10/0; #SHOWME $result
10
 Given that we could be using a variable for the divisor and sometimes that variable may not be instantiated correctly (and thus be read as zero?) I think we HAVE to detect and report to the user if a division by zero is attempted... 

Back to top 


Scandum Site Admin
Joined: 03 Dec 2004 Posts: 3818

Posted: Tue Oct 11, 2011 4:53 pm Post subject: 


I guess I can let tt++ crash on a division zero. 

Back to top 


Slysven
Joined: 10 Apr 2011 Posts: 369 Location: As "Jomin al'Bara" in WoTMUD or Wiltshire, UK

Posted: Tue Oct 11, 2011 7:02 pm Post subject: 


Seriously?
Ah, looking through the source code there ARE error messages commented out...
Use the source, Luke Skywalker! 

Back to top 


