This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

expr ALWAYS with double?

Old posts that have not been replied to for several years.
Locked
User avatar
arcane
Master
Posts: 280
Joined: Thu Jan 30, 2003 9:18 am
Location: Germany
Contact:

expr ALWAYS with double?

Post by arcane »

hi
ive got this simple script

Code: Select all

bind pub - "=" calc_calc

proc calc_calc {nick userhost hand chan arg} {
	puthelp "PRIVMSG $chan :[expr $arg]"
}
but if a user types "= 3/2", the result is "1" because expr calculates integers. how can i tell expr to ALWAYS use doubles? result should be "1.5". ill have to change $arg, havent i?
User avatar
Papillon
Owner
Posts: 724
Joined: Fri Feb 15, 2002 8:00 pm
Location: *.no

Post by Papillon »

yes you do
Elen sila lúmenn' omentielvo
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

It should type:"3 / 2" :)
Once the game is over, the king and the pawn go back in the same box.
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

Just put "1.0 *' at the beginning of the expr and it'll always be double.

% expr "1.0 * 3/2"
1.5
User avatar
arcane
Master
Posts: 280
Joined: Thu Jan 30, 2003 9:18 am
Location: Germany
Contact:

Post by arcane »

Code: Select all

puthelp "PRIVMSG $chan :[expr (1.0 * ($arg))]"
"= 3/2":
it just puts "1.0"...

but doesnt really matter. was just a test.
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

You must have put parenthesis around the integer part. Expr follows order of operations. All you have to do is:

puthelp "PRIVMSG $chan :[expr 1.0 * $arg]"

You could also do a regsub which adds a .0 to all integers in the expression.

But if you are using $arg as input from a user, be careful, because expr will evaluate commands in brackets.

e.g.
% set arg {3 + [hello]}
3 + [hello]
% expr $arg
invalid command name "hello"
Locked