2

I am really confused using math.js's bignumber function. When I use their examples in both the console, and within script tags, math.bignumber() returns a Decimal object. How do I use this object to get an actual float to use?

Example code:

math.add(math.bignumber(0.1), math.bignumber(0.3))

Returns:

Decimal {s: 1, e: -1, d: [5000000]}

Am I doing something wrong? Is there something wrong in the math.js file I downloaded?

E.Arrowood
  • 750
  • 7
  • 17
  • 3
    Isn't the whole point of a big number library to do math that can't be represented in a native number type? – Alexander O'Mara Apr 23 '16 at 17:34
  • Oh I guess i do not understand the purpose of a bignumber library then (Kinda new to this stuff). I am trying to model Simpsons Rule and I want it to be more accurate than just 16 decimal places (like I'm trying to get to 64). You can see my code at ethanarrowood.com/simsponsrule/script.js – E.Arrowood Apr 23 '16 at 17:39
  • Perhaps you want to output a string then? – Alexander O'Mara Apr 23 '16 at 17:39

2 Answers2

3

The examples don't use console.log but print which is defined as follows

function print (value) {
  console.log(math.format(value));
}

If you use print, it should work

lipp
  • 5,586
  • 1
  • 21
  • 32
0

const result = math.add(math.bignumber(0.1), math.bignumber(0.3)) console.log(math.format(result)) // it will give you 0.4

arga wirawan
  • 217
  • 1
  • 2
  • 14