import { Constant } from "./const"; import { makeBigInt, type Integer, type IntegerLike } from "./integer"; type FloatLike = Float | IntegerLike; export class Float extends Constant { public readonly man: bigint; public readonly exp: bigint; constructor(man: IntegerLike, exp: IntegerLike = 0) { super(); man = makeBigInt(man); exp = makeBigInt(exp); while (man % 10n === 0n) { man /= 10n; exp += 1n; } this.man = man; this.exp = exp; } public add(other: FloatLike) { const otherDec = makeFloat(other); if (otherDec.exp === this.exp) return new Float(this.man + otherDec.man, this.exp); } public equals(other: FloatLike) { const otherDec = makeFloat(other); return otherDec.exp === this.exp && otherDec.man === this.man; } public gt(other: FloatLike) { const otherDec = makeFloat(other); if (otherDec.exp > this.exp) return true; if (otherDec.exp < this.exp) return false; return otherDec.man > this.man; } public toString(): string { let str = this.man.toString(); if (this.exp < 0) { const arr = str.split(''); arr.splice(str.length + Number(this.exp), 0, '.'); str = arr.join(''); } else if (this.exp > 0) { for (let i = 0n; i < this.exp; i++) { str += '0'; } } return str; } } function makeFloat(value: FloatLike) { if (value instanceof Float) return value; if (typeof value === 'number') value = value.toString(); if (typeof value === 'string') { if (!value.includes('.')) value += '.0'; } return new Float(value); }