MCPcopy Create free account
hub / github.com/Effect-TS/effect / fromNumber

Function fromNumber

packages/effect/src/Rational.ts:282–297  ·  view source on GitHub ↗
(n: number)

Source from the content-addressed store, hash-verified

280 * @since 4.1.3
281 */
282export const fromNumber = (n: number): Option.Option<Rational> => {
283 if (!Number.isFinite(n)) {
284 return Option.none()
285 }
286 if (n === 0) {
287 return Option.some(zero)
288 }
289 // Doubling is exact, and a finite double becomes integral within 1074 steps.
290 let scaled = n
291 let exponent = 0
292 while (!Number.isInteger(scaled)) {
293 scaled *= 2
294 exponent++
295 }
296 return Option.some(reduce(BigInt(scaled), bigint1 << BigInt(exponent)))
297}
298
299/**
300 * Creates a `Rational` from a finite `number`.

Callers 1

fromNumberUnsafeFunction · 0.70

Calls 3

someMethod · 0.80
reduceFunction · 0.70
BigIntInterface · 0.70

Tested by

no test coverage detected