X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Fmath.rb;h=a6baa7090bb014b737776abbbd85d49906fb41b0;hb=38ce7c2531807e9575a207d9c0c7594778946c89;hp=9dd35c985fabdb2f53be5e942b1a4644aab6a4f5;hpb=282909e0ef97ab3045254920dcee5eab2be25505;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/math.rb b/data/rbot/plugins/math.rb index 9dd35c98..a6baa709 100644 --- a/data/rbot/plugins/math.rb +++ b/data/rbot/plugins/math.rb @@ -39,12 +39,17 @@ class MathPlugin < Plugin expr.gsub!(/\b#{k}\b/, v) } + # ruby doesn't like floating-point values without a 0 + # in front of them, so find any non-digit followed by + # a . and insert a 0 before the . + expr.gsub!(/(\D|^)(\.\d+)/,'\10\2') + while expr =~ /(exp ([\w\d]+))/ exp = $1 val = Math.exp($2).to_s expr.gsub!(/#{Regexp.escape exp}/, "+#{val}") end - + while expr =~ /^\s*(dec2hex\s*(\d+))\s*\?*/ exp = $1 val = sprintf("%x", $2) @@ -56,13 +61,13 @@ class MathPlugin < Plugin while expr =~ /(log\s*((\d+\.?\d*)|\d*\.?\d+))\s*/ exp = $1 res = $2 - + if res == 0 val = "Infinity" else val = Math.log(res).to_s end - + expr.gsub!(/#{Regexp.escape exp}/, "+#{val}") end