New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support operations with string+bignumber and string+fraction #789

Closed
josdejong opened this Issue Feb 2, 2017 · 12 comments

Comments

Projects
None yet
3 participants
@josdejong
Owner

josdejong commented Feb 2, 2017

Adding a string to a number or boolean works:

math.add("2", 4)     // 6
math.add("2", true)  // 3

Whilst trying to add a string and a bignumber, fraction, or complex number fails:

math.add("2", math.bignumber(4))    // TypeError: Unexpected type of argument in function add (expected: number, actual: BigNumber, index: 1)
math.add("2", math.fraction(2, 4))  // TypeError: Unexpected type of argument in function add (expected: number, actual: Fraction, index: 1)
math.add("2", math.complex(2, 0))   // TypeError: Unexpected type of argument in function add (expected: number, actual: Complex, index: 1)

Reason is that there are a few conversion definitions missing: string to bignumber, string to fraction, string to complex, and maybe a few more. See code.

@Snorvarg

This comment has been minimized.

Snorvarg commented Feb 3, 2017

Would it be easy for me to add your proposed fix on my local copy of math.js? If it works, I can upload the fix here, if that would help in any way.

@josdejong

This comment has been minimized.

Owner

josdejong commented Feb 3, 2017

Should be fixed now in the develop branch, see this commit: 41f4cd6

Would be great if you could give it a try and see whether I didn't overlook anything.

@Snorvarg

This comment has been minimized.

Snorvarg commented Feb 6, 2017

Works! This produces the answer 10:

var bigmath = math.create({
  number: 'BigNumber',  
  precision: 32
});

vars = {a : 12, b : 3};
ans = bigmath.eval('a/b + a/2', vars); 

Thank's a lot for the quick fix.

@josdejong josdejong closed this in 41f4cd6 Feb 6, 2017

@josdejong

This comment has been minimized.

Owner

josdejong commented Feb 6, 2017

👍 I've just released v3.9.1 containing this fix.

@Snorvarg

This comment has been minimized.

Snorvarg commented Feb 7, 2017

Is there a way to ask mathjs for it's version 'code-wise' ?

I have mathjs installed in nodejs on a server, and installed it with npm. When I ask npm, it tells me version 3.9.1 is installed, but for some unknown reasons your fix don't seem to come through.

...I was just thinking, if I can ask something like "math.version();" in my javascript-file, I would know for sure.

@ericman314

This comment has been minimized.

Collaborator

ericman314 commented Feb 7, 2017

@Snorvarg

This comment has been minimized.

Snorvarg commented Feb 8, 2017

Yep, that worked. But it says "3.9.0" even though I downloaded "3.9.1".

@josdejong

This comment has been minimized.

Owner

josdejong commented Feb 8, 2017

I see I forgot to update the website, the latest version was only available on npm. Should be fixed now.

Sorry for the inconvenience...

@josdejong

This comment has been minimized.

Owner

josdejong commented Feb 8, 2017

Not sure though why this would happen when installing via npm

@Snorvarg

This comment has been minimized.

Snorvarg commented Feb 8, 2017

When I installed it with npm, it now says "3.9.1", so npm is working fine.

However, I have tricked myself; there seem to be some problems still. This example I gave you earlier are working:

var bigmath = math.create({
  number: 'BigNumber',  
  precision: 32
});

vars = {a : 12, b : 3};
ans = bigmath.eval('a/b + a/2', vars); 

But this, with bigger numbers, still produces the same error:

var bigmath = math.create({
  number: 'BigNumber',  
  precision: 32
});

vars = {a : 257, b : 936};
ans = bigmath.eval('a/b + a/2', vars); 
@josdejong

This comment has been minimized.

Owner

josdejong commented Feb 8, 2017

You're touching a different issue now The error you get there is:

Cannot implicitly convert a number with >15 significant digits to BigNumber (value: 0.2745726495726496). Use function bignumber(x) to convert to BigNumber

This error warns you that you're thinking you working with BigNumbers whilst you aren't: you're mixing numbers and BigNumbers which resulted in loss of precision. Reason is that in your expression the variables a and b are numbers, whilst the constant 2 is created as a BigNumber so a/b returns a number, a/2 returns a BigNumber, and adding a number to a BigNumber then trows this error.

I think the best solution is to convert your input variables a and b into BigNumbers first:

var bigmath = math.create({
  number: 'BigNumber',  
  precision: 32
});

vars = {a : math.bignumber(257), b : math.bignumber(936)};
// or: 
// vars = {a : bigmath.eval('257'), b : bigmath.eval('936')};
ans = bigmath.eval('a/b + a/2', vars); 

Though there is a good reason for this, I realize that it works inconvenient. Let's think wether we can come up with a nicer solution to work in "BigNumber" mode.

@Snorvarg

This comment has been minimized.

Snorvarg commented Feb 9, 2017

This works, and that's great. The only thing needed is a note about it in the documentation about BigNumbers, I don't believe there is.

http://mathjs.org/docs/datatypes/bignumbers.html

In there could be a simple example, why not the very example you supplied here? :)

var bigmath = math.create({
  number: 'BigNumber'
});

vars = {a : math.bignumber(257), b : math.bignumber(936)};

ans = bigmath.eval('a/b + a/2', vars); 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment