I'm looking for a simple calculator. Project GConvert seems to be faded, and even if it hadn't been, I'd still like a version that is not depending on Google for its math.
The closest thing I've come is "calc.tcl" (http://briped.net/2007/06/20/calctcl/), which seemed to be exactly what I wanted. I'm pleased with everything with it, except how it parses decimals in division sums.
Where 123/2 is really 61.5. So, is there any simple way to make the script include decimals? I understand that it would be complicated with division math where the result is infinite, eg. 16/9=1.7777777.... But in this case, perhaps a round off to like say two or there decimals would suffice. Any chance for this? Without tons of coding? Perhaps Tcl even comes with simple math functions? If so, this wouldn't be too complicated?
01:33:07 <@Gewan> !c 123/2
01:33:07 <@Eggdrop> 123/2 = 61
01:33:10 <@Gewan> !c 123/2.0
01:33:11 <@Eggdrop> error calculating '123/2.0' (syntax error in expression "123/2.0": extra tokens at end of expression)
01:33:17 <@Gewan> !c 123/2,0
01:33:20 <@Eggdrop> error calculating '123/2,0' (syntax error in expression "123/2,0": extra tokens at end of expression)
The errors you are getting are as a consequence of how the script you are using treats the command arguments. I was merely illustrating how core Tcl handles math.
The script is on an Eggdrop version 1.6.20 using Tcl 8.5
There is some discussion on google regarding the script but this is more to do with the difference in default decimal precision between Tcl 8.4 and Tcl 8.5, rather than the generation of errors.
Hopefully somebody following this thread will recognise the exact error and point you in the right direction. I would download the file again and ensure that you use a competent text editor to read/modify it. I suggest you do not modify it in any way until you have established whether or not it works for you as is.
@arfer, Thank you very much for you taking the time to check the script. Very kind.
I tried copying the script again, pasting it into vim. Same error again though :/
It works fine without use of decimals, but the 123/2.0 won't fly.
Also, for your information:
I am Eggdrop, running eggdrop v1.6.19+ctcpfix: 6 users (mem: 267k).
Tcl version: 8.4.19 (header version 8.4.19)
--EDIT--
Guys! Never mind this.
I did a simple .restart and guess what?
Yeah, it now works like a charm.
Thanks for all your help!
It won't even round off per default, but just omit the decimals, see. Adding a simple dot will make it return correct answer. Any chance that anyone here could modify calc.tcl so that it add the dot auto matically? When I'm tired I forget to add the dot, and I'm too slow/dumb to always "see" when decimals are to be expected in a result (please don't judge me).
So, anyone feeling bored? Please make me a "calc_mod.tcl" that will add the "." (in either numerator or denominator) per default. I would be _very_ happy!
Gewan wrote:So, anyone feeling bored? Please make me a "calc_mod.tcl" that will add the "." (in either numerator or denominator) per default. I would be _very_ happy!
Using the original script make these simple modifications:
change this:
} elseif {[catch {expr [calc_fixexpr $remainder]} output]} {
to this:
} elseif {[catch {set output [format "%.${::calc_precision}f" [expr [calc_fixexpr [calc_force_floats $remainder]]]]} output]} {
change this:
calc_msg $output2 "$remainder = $output"
to this:
calc_msg $output2 "$remainder = [calc_precision_format $output]"
then add these procedures to the end:
proc calc_precision_format {value} {
set v [split $value .]
set v1 [lindex $v 0]
set v2 [string trim [lindex $v 1] " 0"]
if {[string length $v2]} { set v "${v1}.${v2}" } { set v $v1 }
}
proc calc_force_floats {equation} {
regsub -all -- {[0-9.]+} $equation "double(\\0)" equation
return $equation
}
then add this to the very top of the script:
# set the digits of precision you want in your display
set calc_precision 7
This will remove the ending 0's, as well as the decimal point when not needed. Using the variable calc_precision you control how many digits will be the maximum after the decimal point. If this looks familiar, yes, this is the exact same technique used within Incith:Weather.
You will never see things like ( 7.0200000 or 7.0000000 ) .. These will instead be displayed as ( 7.02 or 7 ) respectively. This is because even though you've set calc_precision to 7, it will smartly clean the number up before displaying to remove needless precision.
Try this out and see if it works
edit: corrected issue mentioned below....
Last edited by speechles on Wed Mar 09, 2011 11:20 am, edited 2 times in total.
Script has worked flawlessly over the years.
However, now something's messed up.
I'm not sure if auto-updates ('Buntu) over apt-get has caused it.
Anyhow, with my latest revision, all !c requests yields answers in /MSG.
I was dazed and angry and worked my way for a solution -- empty handed.
Niow, I found this simple script that may be a better scratch than the one we used.
It works nice (decimals and all) by default, but it's adding ".0" for integers.
Is there any easy way to modify this script so that it returns integers without .0 and I would fly of happyness! :>
-- These last two demonstrate how precision affects results ---
<speechles> !calc 2.000005+9.38712317823781287817237812789378912318923*5
<sp33chy> 2.000005+9.38712317823781287817237812789378912318923*5 = 48.9356209
<speechles> !calc 48.9356209 - 5*9.38712317823781287817237812789378912318923
<sp33chy> 48.9356209 - 5*9.38712317823781287817237812789378912318923 = 2.5
That should be "2.000005" but because we restrict precision to 7, we wind up off by .5 or so .. This is normal because we create the condition by stripping excess digits. You need to take this into account when you set your precision. 7 seems a good number. Maybe 8 is better. Who knows?
Enjoys ;^)
Uses same procedures used to modify other script to same behavior.
Last edited by speechles on Fri Jul 13, 2012 7:20 pm, edited 1 time in total.