[CustomAPI] Math expressions

Asked May 24, 2016·36 replies·Last activity 7 years ago·Open in Discord ↗
Archived from the original community forum
Things may have changed since it was written. Still stuck? Ask the community on Discord.

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.

VHqmycy.png

36 replies

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:

<img src='>

thZjya6.png

can you add an atan2 function?

and can you let the F55-F80 not appear the Scientific notation?
[img][/img]

02e576f647.jpg

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.

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.

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

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|.

d8578e58dd21af27.jpg

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.

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?

The problem is the Twitch variable for followers uses a comma which is actually why I was asking for the option to strip them. ;)

Sample code if commas were not a problem:
$(urlfetch ttp://twitch.center/customapi/math?expr=1000000-$(twitch channelname "{{followers}}"))

You can actually strip commas with the $(eval) variable, though it will get rather wordy.

$(urlfetch http://twitch.center/customapi/math?expr=1000000-$(eval "$(twitch channelname "{{followers}}")".replace(/,/g, "")))

That will strip the commas before sending them to the variable.

Edit: You could actually do this with just eval if you wanted. Like so:

$(eval 1000000-"$(twitch channelname "{{followers}}")".replace(/,/g, ""))

Finally got around to messing with that command again. I had no idea $(eval) was a thing! I'll certainly have to keep that one in mind for some future weird commands.

Thanks for the tip, Slikrick!

well it's seem have problem in the point part

the correct display will be 2500.68743,right?

2017-05-25_171227.jpg2017-05-25_170946.jpg

So could this math API do for example 10 minus 15%? I am a creative streamer and it would be handy to work out the shrinkage rate for my clay while I am making on stream.

Assuming you mean you lose 15% of 10, then that would just be the math of 10 - (10 * .15) unless im not understanding what you mean exactly.

yeah that's what I meant. Thank you

There is an error with the Log function. Any results that have a 0 after the decimal would remove the decimal point and the 0. Example:
http://twitch.center/customapi/math?expr=log10(3.1622*32) should give 2.005139, but it gives 25139.

I guess this is actually a bug for any answer that has 0's after the decimal point. (example: 0.1*0.1 gives 010000)

You need to URL encode your query.

* URL encoded is *
- URL encoded is -
+ URL encoded is %2B

$(urlfetch http://twitch.center/customapi/math?expr=5%2B4)

Alternatively you can just use Nightbot's eval variable to get the sum of two numbers:
$(eval 5+4)

Ivory Mouse said:
$(eval 5+4)

I did not know about the eval command. Thank you very much, this works perfectly for what I am doing.

tan(2*pi) = 0
1/tan(2*pi) = 1/0 = ???
BUT api says
tan(2*pi) = -0
1/tan(2*pi) = -4082809838298842.5
1/-0 = ???

that is a problem i think....

@Graceful Cow There is nothing really wrong about any of those mathematical statements.

If you graph the function f(x)=1/tan(2x) you'll see that as x approaches π from the left, the function explodes towards -infinity. That tells you that 1/tan(2π) is undefined.

But because π is an irrational number, the API can only approximate its value. It seems that the API approximates the value of π just slightly below its actual value, therefore 1/tan(2π) returns a very large negative value, but not infinity/-infinity/undefined.

Right, technically, 1/0 = ∞, but here tan(2/pi), due to precision errors, returns 0.0000000000000002, and therefore you end up with a really large number instead of infinity.

Oh right. I forgot that numbers are approximate.
My bad. Sorry.

Didn't find your answer? The community is on Discord.

Ask on Discord