Giter Club home page Giter Club logo

Comments (7)

munrocket avatar munrocket commented on August 17, 2024

Seems that .toExponential() is drunk or I something broke in latest versions.

const D = require('double.js');
const inner_radius = new D('1.0')
console.log(inner_radius.toNumber())
const outer_radius = new D('2.0')
console.log(outer_radius.toNumber())
const radius = outer_radius.add(inner_radius)
console.log(radius.toNumber())
console.log(D)
console.log(outer_radius)
console.log(inner_radius)
console.log(radius)
1
2
3
function Double()
Double {hi: 2, lo: 0}
Double {hi: 1, lo: 0}

Here 0.3.4

from double.js.

yanovich avatar yanovich commented on August 17, 2024
$ cat a.js
import * as Double from 'double.js/dist/double.js'
const D = Double.default.Double

console.log(D)

const inner_radius = new D('1.0')
console.log(inner_radius.toNumber())
const outer_radius = new D('2.0')
console.log(outer_radius.toNumber())
const radius = outer_radius.add(inner_radius)
console.log(radius.toNumber())
console.log(outer_radius)
console.log(inner_radius)
console.log(radius)
$ node --experimental-modules a.mjs 
[Function: Double]
1
2
3
Double { hi: 2, lo: 0 }
Double { hi: 1, lo: 0 }
Double { hi: 3, lo: 0 }

Yes, it looks like .toExponential() is broken. Thanks!

from double.js.

yanovich avatar yanovich commented on August 17, 2024

https://github.com/munrocket/double.js/blame/master/src/double.ts#L108

You are calling Double.sub22(this, ...) which modifies its 1st argument.

from double.js.

munrocket avatar munrocket commented on August 17, 2024

This can be fixed with this.clone()

from double.js.

yanovich avatar yanovich commented on August 17, 2024

Yes, but it turns out to be far more complicated. I've done some printf debugging:

$ cat a.mjs
import * as Double from 'double.js/dist/double.js'
//import { Double } from 'double.js/dist/double.esm.js'
//require = require("esm")(module)
//const D = require('double.js').Double
const D = Double.default.Double

function toExponential(self, precision) {
  if (precision === undefined)
    precision = 33;
  let result = (self.hi < 0.) ? '-' : '';
  if (isNaN(self.hi))
    return 'NaN';
  if (!isFinite(self.hi))
    return result + 'Infinity';
  if (self.toNumber() == 0.)
    return '0e+0';
  let exp = self.hi.toExponential().split('e')[1];
  let str, nextDigs, shift, isPositive;
  for (let i = 0; i < precision; i += 15) {
    str = self.hi.toExponential().split('e');
    console.log('i: ', i, 'str:', str);
    isPositive = (str[0][0] != '-');
    nextDigs = str[0].replace(/^0\.|\./, '').slice(0, 15);
    console.log('nextDigs: ', nextDigs);
    if (!isPositive)
      nextDigs = nextDigs.slice(1);
    console.log('nextDigs: ', nextDigs);
    shift = D.pow2n(new D(10), parseInt(str[1]) - 14);
    console.log('shift: ', shift);
    D.sub22(self, D.mul21(shift, parseInt(nextDigs) * ((isPositive) ? 1 : -1)));
    console.log('shift: ', shift, 'self: ', self);
    nextDigs = nextDigs.slice(0, precision - i);
    console.log('nextDigs: ', nextDigs);
    result += (i != 0) ? nextDigs : nextDigs.slice(0, 1) + '.' + nextDigs.slice(1);
    console.log('result: ', result);
  }
  return result + 'e' + exp;
}

console.log(D)

const inner_radius = new D('1.0')
console.log(inner_radius.toNumber())
const outer_radius = new D('2.0')
console.log(outer_radius.toNumber())
const radius = outer_radius.add(inner_radius)
console.log(radius.toNumber())
console.log(outer_radius)
console.log(inner_radius)
console.log(radius)
console.log(toExponential(radius))
console.log(radius)

then

$ node --experimental-modules a.mjs 
[Function: Double]
1
2
3
Double { hi: 2, lo: 0 }
Double { hi: 1, lo: 0 }
Double { hi: 3, lo: 0 }
i:  0 str: [ '3', '+0' ]
nextDigs:  3
nextDigs:  3
shift:  Double { hi: 1e-14, lo: 1.1806906454401013e-32 }
shift:  Double { hi: 3e-14, lo: 1.6131425298052268e-30 } self:  Double { hi: 2.99999999999997, lo: 1.980662698042579e-16 }
nextDigs:  3
result:  3.
i:  15 str: [ '2.99999999999997', '+0' ]
nextDigs:  299999999999997
nextDigs:  299999999999997
shift:  Double { hi: 1e-14, lo: 1.1806906454401013e-32 }
shift:  Double { hi: 2.99999999999997, lo: 1.980662698042579e-16 } self:  Double { hi: 0, lo: 0 }
nextDigs:  299999999999997
result:  3.299999999999997
i:  30 str: [ '0', '+0' ]
nextDigs:  0
nextDigs:  0
shift:  Double { hi: 1e-14, lo: 1.1806906454401013e-32 }
shift:  Double { hi: 0, lo: 0 } self:  Double { hi: 0, lo: 0 }
nextDigs:  0
result:  3.2999999999999970
3.2999999999999970e+0
Double { hi: 0, lo: 0 }

from double.js.

yanovich avatar yanovich commented on August 17, 2024

JS default number of digits is "as many digits as necessary". So after the 1st cycle there is only 1 digit, not 15. It may make sense to remove the leading digits one by one, until the remainder is 0, or we reach the limit or 33.

from double.js.

munrocket avatar munrocket commented on August 17, 2024

@yanovich I mentioned you in Special thanks section in readme, because this fix was very important for usage and pretty hardcore.

from double.js.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.