Skip to content

Commit

Permalink
fix #3163: toTex wrongly returning Infinity for large BigNumbers
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed Feb 23, 2024
1 parent 4c42a14 commit 50b15a7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# History


# unpublished changes since 12.4.0

- Fix #3163: `toTex` wrongly returning `Infinity` for large BigNumbers.


# 2024-02-22, 12.4.0

- Feat: implement support for trailing commas in matrices (#3154, #2968).
Expand Down
31 changes: 17 additions & 14 deletions src/expression/node/ConstantNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,27 +140,30 @@ export const createConstantNode = /* #__PURE__ */ factory(name, dependencies, ({
*/
_toTex (options) {
const value = this._toString(options)
const type = typeOf(this.value)

switch (typeOf(this.value)) {
switch (type) {
case 'string':
return '\\mathtt{' + escapeLatex(value) + '}'

case 'number':
case 'BigNumber':
{
if (!isFinite(this.value)) {
return (this.value.valueOf() < 0)
? '-\\infty'
: '\\infty'
}

const index = value.toLowerCase().indexOf('e')
if (index !== -1) {
return value.substring(0, index) + '\\cdot10^{' +
value.substring(index + 1) + '}'
}
case 'BigNumber': {
const finite = type === 'BigNumber' ? this.value.isFinite() : isFinite(this.value)
if (!finite) {
return (this.value.valueOf() < 0)
? '-\\infty'
: '\\infty'
}

const index = value.toLowerCase().indexOf('e')
if (index !== -1) {
return value.substring(0, index) + '\\cdot10^{' +
value.substring(index + 1) + '}'
}

return value
}

case 'Fraction':
return this.value.toLatex()

Expand Down
5 changes: 5 additions & 0 deletions test/unit-tests/expression/node/ConstantNode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ describe('ConstantNode', function () {
assert.strictEqual(new ConstantNode(3).toTex(), '3')
assert.deepStrictEqual(new ConstantNode(3).toTex(), '3')
assert.deepStrictEqual(new ConstantNode(math.bignumber('3')).toTex(), '3')
assert.deepStrictEqual(new ConstantNode(math.bignumber('1.3e7')).toTex(), '1.3\\cdot10^{+7}')
assert.deepStrictEqual(new ConstantNode(math.bignumber('1e500')).toTex(), '1\\cdot10^{+500}')
assert.deepStrictEqual(new ConstantNode(math.bignumber('1e-500')).toTex(), '1\\cdot10^{-500}')
assert.deepStrictEqual(new ConstantNode(math.bignumber('12345678901234567890')).toTex(),
'1.234567890123456789\\cdot10^{+19}')
assert.strictEqual(new ConstantNode('hi').toTex(), '\\mathtt{"hi"}')
assert.strictEqual(new ConstantNode(true).toTex(), 'true')
assert.strictEqual(new ConstantNode(false).toTex(), 'false')
Expand Down

0 comments on commit 50b15a7

Please sign in to comment.