[CustomAPI] Math expressions

This is a pretty simple and self explanatory command. You pass it a mathematical expression and it spits out the result.

Installation

To use it, you want to add the following to a command:
$(urlfetch http://twitch.center/customapi/math?expr=$(querystring))

For example, you can have:
!commands add !math $(urlfetch http://twitch.center/customapi/math?expr=$(querystring))

If you’d like to repeat the original query in the output, add a "$(query) = " before the urlfetch.

Examples

Extra detail

The command will return an error if no input is given, and will return “???” if it fails to parse the expression.

It supports:

  • basic arithmetic operations (+, -, *, /, ^)
  • trigonometric operations (sin, cos, tan, …)
  • basic functions such as round, sgn, abs, trunc, log, sqrt
  • constants such as PI and E

Bugs and features

If you have any suggestions or bug reports, just leave a message in this thread and I’ll try to address them.

5 Likes

What do you mean it won’t allow you? What does it tell you when you attempt to do so? It works just fine for me:

1 Like

can you add an atan2 function?

and can you let the F55-F80 not appear the Scientific notation?

1 Like

Had to add tuple support to get binary operations but atan2 should work now. As well as other binary operations such as log(value, base).

The other one is a tricky one: I do a check to see if the result is integer, so that you get 5 instead of 5.0. Large floating numbers always show in scientific notation, but once they are big enough, the precision of the number is not high enough to represent the full number so you don’t get anything after the comma and it assumes it’s an integer.

But I think I can just make everything show full form.

1 Like

ok ata2 is work
but now 1/sqrt(5) doesn’t work,can’t calculation correct Fibonacci number

Erm, fucked up with my implementation, try now it should work.

1 Like

ok it’s work thanks!
and hope you can make it without display any scientific notation in the end
because i prefer see the full number

and ​log1p function doesn’t work… ​log1p(x) = log(x+1) = ln(x+1)
you also can add base system in this funcion such as log1p(1023,1)
it will be 10

btw if you can add constant phi it will be more easier on calculation Fibonacci number
phi=(1+sqrt(5))/2

log1p worksfine . I can’t add base system, but log change of base formula can take you from any base to any other easily.
Also fixed scientific notation.

Everything that’s available is here: https://docs.python.org/2/library/math.html

ok log1p now is work,so can you add a phi constant?
about the scientific notation problem i think you can use setprecision(200) or printf(“x = %200lf\n”, x);
I need at least 200 digit output because that is youtube maximum output
if number over it,that just use scientific notation

There is a problem with copysign(x,y).

This function will return |x| with the sign of y.
EX:
copysign(1.0,1.0) = 1.0
copysign(-2.0,10.0) = 2.0
copysign(10.0,-0.1) = -10.0
coptsign(-5.0,-0.0)= -5.0

But it just return |x|.

Yes,you’re right, also many function are disabled
such as exp,expm1,erf…

I believe most of those are from python 2.7 and my server is running 2.6

Why do you need exp(x), just do e**x, also what are you guys even using this api for? It’s meant for quick in chat calculation, why would you need to do advanced calculations in chat?!

well,those function disabled are ok but i really need the full number output

btw didn’t you know the “^” sign is equal **??

Just curious, what are you using this customapi for? For any real tasks an actual calculator should be used, and for any advanced calculations a more local solution is generally used. Its really only meant for quick one-off calculations through chat, as ehsankia mentioned.

1 Like

well just do the base and advanced calculations

and running a expedition system

I have two requests.

  1. Optionally ignore commas so that you can still do math such as 1,234+100.
  2. Optionally add commas to the output.

I used to ignore commas, but then I added binary functions like atan2, so I needed to include them again.

Would you consider adding an optional flag such as “stripcomma”?
http://twitch.center/customapi/math?stripcommas=1&expr=1,245+100
That way, people have the option to loose the more advance features like atan2 if the need to strip commas but by default, everything would work as it does now.

The thousand separator could be a similar option (thousep=, for instance).

Is there any way to implement a constant, I wanted to create a command that uses this API to see how many followers someone needs to reach 1000000 followers, how can I do this if possible?

You can use the twitch variable inside the math variable:
https://docs.nightbot.tv/commands/variables/twitch