BigFloat

Uses BigInt to make BigFloats

Αυτός ο κώδικας δεν πρέπει να εγκατασταθεί άμεσα. Είναι μια βιβλιοθήκη για άλλους κώδικες που περιλαμβάνεται μέσω της οδηγίας meta // @require https://update.greasyfork.org/scripts/563676/1740238/BigFloat.js

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

Δημιουργός
NotYou
Έκδοση
1.0.0
Δημιουργήθηκε την
22/01/2026
Ενημερώθηκε την
22/01/2026
Μέγεθος
9 KB
Άδεια
MIT

BigFloat

Description

BigFloat library was created to avoid IEEE754 floating point numbers, meaning more accurate results and ability to work with larger (actually sometimes smaller) values. BigFloat uses esnext feature called BigInt to implement this solution.

Usage Examples

console.log(new BigFloat(0.123456789).add(0.987654321).toNumber({ precision: 9 })) // 1.11111111

console.log(new BigFloat(0.1).add(0.2).multiply(3).toNumber()) // 0.9

// vs using built-in operators

console.log(0.12345678 + 0.987654321) // 1.1111111009999999

console.log((0.1 + 0.2) * 3) // 0.9000000000000001

Instance Properties

BigFloat.prototype[Symbol.toStringTag]

The value of [Symbol.toStringTag] is "BigFloat" (which is the name of the class).

Note: BigFloat also has stringify instance method, which converts the value to the string.

Instance Methods

Note

AcceptableValue is number | (backtick)${number}(backtick) | BigInt | InstanceType<typeof BigFloat>

I wrote ` as (backtick) because markdown doesn't allow me to write template strings is readable way.

BigFloat.prototype.getRawValue

@returns {bigint}

Returns the value of the big float as a raw bigint

console.log(new BigFloat('12.34').getRawValue()) // 1234n

BigFloat.prototype.getScale

@returns {number}

Returns the scale of the value

console.log(new BigFloat('12.34').getScale()) // -2

BigFloat.prototype.stringify

@param {Optional<{

trailingZero: boolean, // default: true

headZero: boolean, // default: true

precision: number, // default: 3

fixedPointNotation: boolean // default: false

}>} options

@returns {string}

Stringifies big float

console.log(new BigFloat(12.34).stringify()) // '12.34'
console.log(new BigFloat(1).stringify()) // '1.0'
console.log(new BigFloat(1.123456789).stringify()) // '1.123' (default precision is 3)
console.log(new BigFloat(1.123456789).stringify({ precision: 9 })) // '1.123456789'
console.log(new BigFloat(1).stringify({ trailingZero: false })) // '1'
console.log(new BigFloat(0.123).stringify({ headZero: false })) // '.123'
console.log(new BigFloat(1).stringify({ precision: 5, fixedPointNotation: true })) // '1.00000'

BigFloat.prototype.toNumber

@param {Optional<{

precision: number // default: 3

}>} options

@returns {number}

Firstly stringfies the number using stringify (with default params) method, and then converts it to number

console.log(new BigFloat('12.34').toNumber()) // 12.34
console.log(new BigFloat('1.0').toNumber()) // 1
console.log(new BigFloat('1.12345689').toNumber()) // 0.123 (default precision is 3)
console.log(new BigFloat('1.12345689').toNumber({ precision: 9 })) // 1.123456789

BigFloat.prototype.add

@param {AcceptableValue} value

@returns {this}

console.log(new BigFloat('1.1').add('2.2').toNumber()) // 3.3

BigFloat.prototype.subtract

@param {AcceptableValue} value

@returns {this}

console.log(new BigFloat('1.1').subtract('2.2').toNumber()) // -1.1

BigFloat.prototype.multiply

@param {AcceptableValue} value

@returns {this}

console.log(new BigFloat('3.333').multiply('3.333').toNumber({ precision: 6 })) // 11.108889

BigFloat.prototype.divide

@param {AcceptableValue} value

@param {number} precision default is 3

@returns {this}

console.log(new BigFloat('3.333').divide('9', 10).stringify({ precision: 10 })) // 0.3703333333