goscript 0.0.34 → 0.0.35
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/compiler/analysis.go +28 -20
- package/compiler/analysis_test.go +14 -0
- package/compiler/assignment.go +19 -7
- package/compiler/compiler.go +15 -99
- package/compiler/composite-lit.go +60 -17
- package/compiler/decl.go +1 -1
- package/compiler/expr-call.go +233 -35
- package/compiler/expr-selector.go +28 -2
- package/compiler/expr.go +13 -37
- package/compiler/lit.go +111 -2
- package/compiler/primitive.go +6 -6
- package/compiler/protobuf.go +0 -5
- package/compiler/sanitize.go +101 -0
- package/compiler/spec-value.go +25 -18
- package/compiler/stmt-assign.go +22 -1
- package/compiler/stmt.go +26 -9
- package/compiler/type.go +3 -3
- package/dist/gs/builtin/builtin.d.ts +3 -2
- package/dist/gs/builtin/builtin.js +2 -1
- package/dist/gs/builtin/builtin.js.map +1 -1
- package/dist/gs/builtin/map.js.map +1 -1
- package/dist/gs/builtin/slice.d.ts +7 -1
- package/dist/gs/builtin/slice.js +112 -22
- package/dist/gs/builtin/slice.js.map +1 -1
- package/dist/gs/github.com/pkg/errors/errors.d.ts +13 -0
- package/dist/gs/github.com/pkg/errors/errors.js +232 -0
- package/dist/gs/github.com/pkg/errors/errors.js.map +1 -0
- package/dist/gs/github.com/pkg/errors/go113.d.ts +4 -0
- package/dist/gs/github.com/pkg/errors/go113.js +34 -0
- package/dist/gs/github.com/pkg/errors/go113.js.map +1 -0
- package/dist/gs/github.com/pkg/errors/index.d.ts +3 -0
- package/dist/gs/github.com/pkg/errors/index.js +4 -0
- package/dist/gs/github.com/pkg/errors/index.js.map +1 -0
- package/dist/gs/github.com/pkg/errors/stack.d.ts +32 -0
- package/dist/gs/github.com/pkg/errors/stack.js +111 -0
- package/dist/gs/github.com/pkg/errors/stack.js.map +1 -0
- package/dist/gs/maps/index.d.ts +2 -2
- package/dist/gs/maps/index.js +2 -2
- package/dist/gs/maps/index.js.map +1 -1
- package/dist/gs/maps/iter.d.ts +7 -0
- package/dist/gs/maps/iter.js +57 -0
- package/dist/gs/maps/iter.js.map +1 -0
- package/dist/gs/maps/maps.d.ts +7 -0
- package/dist/gs/maps/maps.js +67 -0
- package/dist/gs/maps/maps.js.map +1 -0
- package/dist/gs/math/abs.gs.d.ts +1 -0
- package/dist/gs/math/abs.gs.js +10 -0
- package/dist/gs/math/abs.gs.js.map +1 -0
- package/dist/gs/math/acosh.gs.d.ts +2 -0
- package/dist/gs/math/acosh.gs.js +14 -0
- package/dist/gs/math/acosh.gs.js.map +1 -0
- package/dist/gs/math/asin.gs.d.ts +4 -0
- package/dist/gs/math/asin.gs.js +24 -0
- package/dist/gs/math/asin.gs.js.map +1 -0
- package/dist/gs/math/asinh.gs.d.ts +2 -0
- package/dist/gs/math/asinh.gs.js +14 -0
- package/dist/gs/math/asinh.gs.js.map +1 -0
- package/dist/gs/math/atan.gs.d.ts +4 -0
- package/dist/gs/math/atan.gs.js +22 -0
- package/dist/gs/math/atan.gs.js.map +1 -0
- package/dist/gs/math/atan2.gs.d.ts +2 -0
- package/dist/gs/math/atan2.gs.js +30 -0
- package/dist/gs/math/atan2.gs.js.map +1 -0
- package/dist/gs/math/atanh.gs.d.ts +2 -0
- package/dist/gs/math/atanh.gs.js +16 -0
- package/dist/gs/math/atanh.gs.js.map +1 -0
- package/dist/gs/math/bits.gs.d.ts +5 -0
- package/dist/gs/math/bits.gs.js +46 -0
- package/dist/gs/math/bits.gs.js.map +1 -0
- package/dist/gs/math/cbrt.gs.d.ts +2 -0
- package/dist/gs/math/cbrt.gs.js +14 -0
- package/dist/gs/math/cbrt.gs.js.map +1 -0
- package/dist/gs/math/const.gs.d.ts +30 -0
- package/dist/gs/math/const.gs.js +61 -0
- package/dist/gs/math/const.gs.js.map +1 -0
- package/dist/gs/math/copysign.gs.d.ts +1 -0
- package/dist/gs/math/copysign.gs.js +20 -0
- package/dist/gs/math/copysign.gs.js.map +1 -0
- package/dist/gs/math/dim.gs.d.ts +5 -0
- package/dist/gs/math/dim.gs.js +69 -0
- package/dist/gs/math/dim.gs.js.map +1 -0
- package/dist/gs/math/erf.gs.d.ts +4 -0
- package/dist/gs/math/erf.gs.js +336 -0
- package/dist/gs/math/erf.gs.js.map +1 -0
- package/dist/gs/math/erfinv.gs.d.ts +2 -0
- package/dist/gs/math/erfinv.gs.js +118 -0
- package/dist/gs/math/erfinv.gs.js.map +1 -0
- package/dist/gs/math/exp.gs.d.ts +5 -0
- package/dist/gs/math/exp.gs.js +30 -0
- package/dist/gs/math/exp.gs.js.map +1 -0
- package/dist/gs/math/expm1.gs.d.ts +2 -0
- package/dist/gs/math/expm1.gs.js +17 -0
- package/dist/gs/math/expm1.gs.js.map +1 -0
- package/dist/gs/math/floor.gs.d.ts +8 -0
- package/dist/gs/math/floor.gs.js +75 -0
- package/dist/gs/math/floor.gs.js.map +1 -0
- package/dist/gs/math/fma.gs.d.ts +1 -0
- package/dist/gs/math/fma.gs.js +8 -0
- package/dist/gs/math/fma.gs.js.map +1 -0
- package/dist/gs/math/frexp.gs.d.ts +2 -0
- package/dist/gs/math/frexp.gs.js +28 -0
- package/dist/gs/math/frexp.gs.js.map +1 -0
- package/dist/gs/math/gamma.gs.d.ts +3 -0
- package/dist/gs/math/gamma.gs.js +149 -0
- package/dist/gs/math/gamma.gs.js.map +1 -0
- package/dist/gs/math/hypot.gs.d.ts +2 -0
- package/dist/gs/math/hypot.gs.js +16 -0
- package/dist/gs/math/hypot.gs.js.map +1 -0
- package/dist/gs/math/index.d.ts +44 -0
- package/dist/gs/math/index.js +45 -0
- package/dist/gs/math/index.js.map +1 -0
- package/dist/gs/math/j0.gs.d.ts +4 -0
- package/dist/gs/math/j0.gs.js +228 -0
- package/dist/gs/math/j0.gs.js.map +1 -0
- package/dist/gs/math/j1.gs.d.ts +4 -0
- package/dist/gs/math/j1.gs.js +211 -0
- package/dist/gs/math/j1.gs.js.map +1 -0
- package/dist/gs/math/jn.gs.d.ts +2 -0
- package/dist/gs/math/jn.gs.js +412 -0
- package/dist/gs/math/jn.gs.js.map +1 -0
- package/dist/gs/math/ldexp.gs.d.ts +2 -0
- package/dist/gs/math/ldexp.gs.js +20 -0
- package/dist/gs/math/ldexp.gs.js.map +1 -0
- package/dist/gs/math/lgamma.gs.d.ts +2 -0
- package/dist/gs/math/lgamma.gs.js +243 -0
- package/dist/gs/math/lgamma.gs.js.map +1 -0
- package/dist/gs/math/log.gs.d.ts +2 -0
- package/dist/gs/math/log.gs.js +16 -0
- package/dist/gs/math/log.gs.js.map +1 -0
- package/dist/gs/math/log10.gs.d.ts +4 -0
- package/dist/gs/math/log10.gs.js +17 -0
- package/dist/gs/math/log10.gs.js.map +1 -0
- package/dist/gs/math/log1p.gs.d.ts +2 -0
- package/dist/gs/math/log1p.gs.js +17 -0
- package/dist/gs/math/log1p.gs.js.map +1 -0
- package/dist/gs/math/logb.gs.d.ts +3 -0
- package/dist/gs/math/logb.gs.js +43 -0
- package/dist/gs/math/logb.gs.js.map +1 -0
- package/dist/gs/math/mod.gs.d.ts +2 -0
- package/dist/gs/math/mod.gs.js +26 -0
- package/dist/gs/math/mod.gs.js.map +1 -0
- package/dist/gs/math/modf.gs.d.ts +2 -0
- package/dist/gs/math/modf.gs.js +24 -0
- package/dist/gs/math/modf.gs.js.map +1 -0
- package/dist/gs/math/nextafter.gs.d.ts +2 -0
- package/dist/gs/math/nextafter.gs.js +66 -0
- package/dist/gs/math/nextafter.gs.js.map +1 -0
- package/dist/gs/math/pow.gs.d.ts +3 -0
- package/dist/gs/math/pow.gs.js +40 -0
- package/dist/gs/math/pow.gs.js.map +1 -0
- package/dist/gs/math/pow10.gs.d.ts +1 -0
- package/dist/gs/math/pow10.gs.js +14 -0
- package/dist/gs/math/pow10.gs.js.map +1 -0
- package/dist/gs/math/remainder.gs.d.ts +2 -0
- package/dist/gs/math/remainder.gs.js +25 -0
- package/dist/gs/math/remainder.gs.js.map +1 -0
- package/dist/gs/math/signbit.gs.d.ts +1 -0
- package/dist/gs/math/signbit.gs.js +5 -0
- package/dist/gs/math/signbit.gs.js.map +1 -0
- package/dist/gs/math/sin.gs.d.ts +4 -0
- package/dist/gs/math/sin.gs.js +29 -0
- package/dist/gs/math/sin.gs.js.map +1 -0
- package/dist/gs/math/sincos.gs.d.ts +1 -0
- package/dist/gs/math/sincos.gs.js +11 -0
- package/dist/gs/math/sincos.gs.js.map +1 -0
- package/dist/gs/math/sinh.gs.d.ts +4 -0
- package/dist/gs/math/sinh.gs.js +27 -0
- package/dist/gs/math/sinh.gs.js.map +1 -0
- package/dist/gs/math/sqrt.gs.d.ts +2 -0
- package/dist/gs/math/sqrt.gs.js +15 -0
- package/dist/gs/math/sqrt.gs.js.map +1 -0
- package/dist/gs/math/tan.gs.d.ts +2 -0
- package/dist/gs/math/tan.gs.js +17 -0
- package/dist/gs/math/tan.gs.js.map +1 -0
- package/dist/gs/math/tanh.gs.d.ts +2 -0
- package/dist/gs/math/tanh.gs.js +17 -0
- package/dist/gs/math/tanh.gs.js.map +1 -0
- package/dist/gs/math/trig_reduce.gs.d.ts +1 -0
- package/dist/gs/math/trig_reduce.gs.js +62 -0
- package/dist/gs/math/trig_reduce.gs.js.map +1 -0
- package/dist/gs/math/unsafe.gs.d.ts +4 -0
- package/dist/gs/math/unsafe.gs.js +47 -0
- package/dist/gs/math/unsafe.gs.js.map +1 -0
- package/dist/gs/strconv/atob.gs.d.ts +4 -0
- package/dist/gs/strconv/atob.gs.js +42 -0
- package/dist/gs/strconv/atob.gs.js.map +1 -0
- package/dist/gs/strconv/atof.gs.d.ts +2 -0
- package/dist/gs/strconv/atof.gs.js +51 -0
- package/dist/gs/strconv/atof.gs.js.map +1 -0
- package/dist/gs/strconv/atoi.gs.d.ts +33 -0
- package/dist/gs/strconv/atoi.gs.js +200 -0
- package/dist/gs/strconv/atoi.gs.js.map +1 -0
- package/dist/gs/strconv/doc.gs.d.ts +1 -0
- package/dist/gs/strconv/doc.gs.js +2 -0
- package/dist/gs/strconv/doc.gs.js.map +1 -0
- package/dist/gs/strconv/ftoa.gs.d.ts +3 -0
- package/dist/gs/strconv/ftoa.gs.js +58 -0
- package/dist/gs/strconv/ftoa.gs.js.map +1 -0
- package/dist/gs/strconv/index.d.ts +6 -0
- package/dist/gs/strconv/index.js +7 -0
- package/dist/gs/strconv/index.js.map +1 -0
- package/dist/gs/strconv/itoa.gs.d.ts +6 -0
- package/dist/gs/strconv/itoa.gs.js +37 -0
- package/dist/gs/strconv/itoa.gs.js.map +1 -0
- package/dist/gs/strconv/quote.gs.d.ts +19 -0
- package/dist/gs/strconv/quote.gs.js +217 -0
- package/dist/gs/strconv/quote.gs.js.map +1 -0
- package/dist/gs/strings/index.d.ts +3 -0
- package/dist/gs/strings/index.js +4 -0
- package/dist/gs/strings/index.js.map +1 -1
- package/dist/gs/strings/replace.d.ts +0 -74
- package/dist/gs/strings/replace.js +6 -204
- package/dist/gs/strings/replace.js.map +1 -1
- package/dist/gs/strings/search.d.ts +0 -1
- package/dist/gs/strings/search.js +0 -21
- package/dist/gs/strings/search.js.map +1 -1
- package/gs/builtin/builtin.ts +9 -7
- package/gs/builtin/map.ts +5 -1
- package/gs/builtin/slice.ts +152 -24
- package/gs/github.com/pkg/errors/errors.ts +307 -0
- package/gs/github.com/pkg/errors/go113.ts +39 -0
- package/gs/github.com/pkg/errors/index.ts +3 -0
- package/gs/github.com/pkg/errors/stack.ts +127 -0
- package/gs/maps/index.ts +2 -2
- package/gs/maps/iter.ts +67 -0
- package/gs/maps/maps.ts +89 -0
- package/gs/math/TODO.md +156 -0
- package/gs/math/abs.gs.test.ts +29 -0
- package/gs/math/abs.gs.ts +13 -0
- package/gs/math/acosh.gs.test.ts +39 -0
- package/gs/math/acosh.gs.ts +21 -0
- package/gs/math/asin.gs.test.ts +66 -0
- package/gs/math/asin.gs.ts +27 -0
- package/gs/math/asinh.gs.test.ts +37 -0
- package/gs/math/asinh.gs.ts +21 -0
- package/gs/math/atan.gs.test.ts +49 -0
- package/gs/math/atan.gs.ts +27 -0
- package/gs/math/atan2.gs.test.ts +55 -0
- package/gs/math/atan2.gs.ts +37 -0
- package/gs/math/atanh.gs.test.ts +47 -0
- package/gs/math/atanh.gs.ts +21 -0
- package/gs/math/bits.gs.test.ts +88 -0
- package/gs/math/bits.gs.ts +61 -0
- package/gs/math/cbrt.gs.test.ts +57 -0
- package/gs/math/cbrt.gs.ts +20 -0
- package/gs/math/const.gs.test.ts +54 -0
- package/gs/math/const.gs.ts +93 -0
- package/gs/math/copysign.gs.test.ts +44 -0
- package/gs/math/copysign.gs.ts +27 -0
- package/gs/math/dim.gs.test.ts +102 -0
- package/gs/math/dim.gs.ts +84 -0
- package/gs/math/erf.gs.test.ts +92 -0
- package/gs/math/erf.gs.ts +409 -0
- package/gs/math/erfinv.gs.test.ts +104 -0
- package/gs/math/erfinv.gs.ts +169 -0
- package/gs/math/exp.gs.test.ts +82 -0
- package/gs/math/exp.gs.ts +39 -0
- package/gs/math/expm1.gs.test.ts +48 -0
- package/gs/math/expm1.gs.ts +23 -0
- package/gs/math/floor.gs.test.ts +146 -0
- package/gs/math/floor.gs.ts +88 -0
- package/gs/math/fma.gs.test.ts +83 -0
- package/gs/math/fma.gs.ts +7 -0
- package/gs/math/frexp.gs.test.ts +146 -0
- package/gs/math/frexp.gs.ts +37 -0
- package/gs/math/gamma.gs.test.ts +66 -0
- package/gs/math/gamma.gs.ts +158 -0
- package/gs/math/hypot.gs.test.ts +73 -0
- package/gs/math/hypot.gs.ts +23 -0
- package/gs/math/index.ts +44 -0
- package/gs/math/j0.gs.test.ts +74 -0
- package/gs/math/j0.gs.ts +257 -0
- package/gs/math/j1.gs.test.ts +81 -0
- package/gs/math/j1.gs.ts +231 -0
- package/gs/math/jn.gs.test.ts +133 -0
- package/gs/math/jn.gs.ts +447 -0
- package/gs/math/ldexp.gs.test.ts +128 -0
- package/gs/math/ldexp.gs.ts +28 -0
- package/gs/math/lgamma.gs.test.ts +102 -0
- package/gs/math/lgamma.gs.ts +251 -0
- package/gs/math/log.gs.test.ts +40 -0
- package/gs/math/log.gs.ts +21 -0
- package/gs/math/log10.gs.test.ts +80 -0
- package/gs/math/log10.gs.ts +25 -0
- package/gs/math/log1p.gs.test.ts +55 -0
- package/gs/math/log1p.gs.ts +24 -0
- package/gs/math/logb.gs.test.ts +87 -0
- package/gs/math/logb.gs.ts +54 -0
- package/gs/math/mod.gs.test.ts +64 -0
- package/gs/math/mod.gs.ts +36 -0
- package/gs/math/modf.gs.test.ts +80 -0
- package/gs/math/modf.gs.ts +32 -0
- package/gs/math/nextafter.gs.test.ts +107 -0
- package/gs/math/nextafter.gs.ts +71 -0
- package/gs/math/pow.gs.test.ts +103 -0
- package/gs/math/pow.gs.ts +55 -0
- package/gs/math/pow10.gs.test.ts +58 -0
- package/gs/math/pow10.gs.ts +19 -0
- package/gs/math/remainder.gs.test.ts +70 -0
- package/gs/math/remainder.gs.ts +33 -0
- package/gs/math/signbit.gs.test.ts +33 -0
- package/gs/math/signbit.gs.ts +8 -0
- package/gs/math/sin.gs.test.ts +83 -0
- package/gs/math/sin.gs.ts +38 -0
- package/gs/math/sincos.gs.test.ts +91 -0
- package/gs/math/sincos.gs.ts +15 -0
- package/gs/math/sinh.gs.test.ts +66 -0
- package/gs/math/sinh.gs.ts +34 -0
- package/gs/math/sqrt.gs.test.ts +49 -0
- package/gs/math/sqrt.gs.ts +20 -0
- package/gs/math/tan.gs.test.ts +50 -0
- package/gs/math/tan.gs.ts +23 -0
- package/gs/math/tanh.gs.test.ts +52 -0
- package/gs/math/tanh.gs.ts +23 -0
- package/gs/math/trig_reduce.gs.ts +66 -0
- package/gs/math/unsafe.gs.ts +52 -0
- package/gs/strconv/atob.gs.ts +45 -0
- package/gs/strconv/atof.gs.ts +60 -0
- package/gs/strconv/atoi.gs.ts +243 -0
- package/gs/strconv/doc.gs.ts +2 -0
- package/gs/strconv/ftoa.gs.ts +66 -0
- package/gs/strconv/index.ts +6 -0
- package/gs/strconv/itoa.gs.ts +41 -0
- package/gs/strconv/quote.gs.ts +245 -0
- package/gs/strings/index.ts +4 -0
- package/gs/strings/replace.ts +9 -237
- package/gs/strings/search.ts +0 -28
- package/package.json +1 -1
- package/gs/maps/iter.gs.ts +0 -71
- package/gs/maps/maps.gs.ts +0 -87
- package/gs/stringslite/godoc.txt +0 -17
- package/gs/stringslite/index.ts +0 -1
- package/gs/stringslite/strings.ts +0 -82
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
import { Erfinv, Erfcinv } from './erfinv.gs.js'
|
|
3
|
+
import { Erf, Erfc } from './erf.gs.js'
|
|
4
|
+
|
|
5
|
+
describe('Erfinv', () => {
|
|
6
|
+
it('should return correct values for zero', () => {
|
|
7
|
+
expect(Erfinv(0)).toBe(0)
|
|
8
|
+
expect(Erfinv(-0)).toBe(-0)
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
it('should return correct values for common inputs', () => {
|
|
12
|
+
// Relax precision tolerance for JavaScript floating-point differences
|
|
13
|
+
expect(Erfinv(0.5)).toBeCloseTo(0.4769362762044699, 6)
|
|
14
|
+
expect(Erfinv(-0.5)).toBeCloseTo(-0.4769362762044699, 6)
|
|
15
|
+
expect(Erfinv(0.8)).toBeCloseTo(0.9061938024368235, 6)
|
|
16
|
+
expect(Erfinv(-0.8)).toBeCloseTo(-0.9061938024368235, 6)
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('should handle boundary values', () => {
|
|
20
|
+
expect(Erfinv(1)).toBe(Number.POSITIVE_INFINITY)
|
|
21
|
+
expect(Erfinv(-1)).toBe(Number.NEGATIVE_INFINITY)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('should return NaN for values outside [-1, 1]', () => {
|
|
25
|
+
expect(Number.isNaN(Erfinv(1.1))).toBe(true)
|
|
26
|
+
expect(Number.isNaN(Erfinv(-1.1))).toBe(true)
|
|
27
|
+
expect(Number.isNaN(Erfinv(2))).toBe(true)
|
|
28
|
+
expect(Number.isNaN(Erfinv(-2))).toBe(true)
|
|
29
|
+
expect(Number.isNaN(Erfinv(Number.NaN))).toBe(true)
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it('should be the inverse of Erf', () => {
|
|
33
|
+
const testValues = [0, 0.1, 0.3, 0.5, 0.7, 0.9, -0.1, -0.3, -0.5, -0.7, -0.9]
|
|
34
|
+
for (const x of testValues) {
|
|
35
|
+
const y = Erfinv(x)
|
|
36
|
+
// Relax precision tolerance for JavaScript floating-point differences
|
|
37
|
+
expect(Erf(y)).toBeCloseTo(x, 5)
|
|
38
|
+
}
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it('should be an odd function', () => {
|
|
42
|
+
const testValues = [0.1, 0.3, 0.5, 0.7, 0.9]
|
|
43
|
+
for (const x of testValues) {
|
|
44
|
+
expect(Erfinv(-x)).toBeCloseTo(-Erfinv(x), 14)
|
|
45
|
+
}
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it('should handle very small values', () => {
|
|
49
|
+
expect(Erfinv(1e-10)).toBeCloseTo(8.862269254527579e-11, 14)
|
|
50
|
+
expect(Erfinv(-1e-10)).toBeCloseTo(-8.862269254527579e-11, 14)
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it('should handle values close to boundaries', () => {
|
|
54
|
+
expect(Erfinv(0.999)).toBeGreaterThan(2)
|
|
55
|
+
expect(Erfinv(-0.999)).toBeLessThan(-2)
|
|
56
|
+
expect(Erfinv(0.9999)).toBeGreaterThan(2.5)
|
|
57
|
+
expect(Erfinv(-0.9999)).toBeLessThan(-2.5)
|
|
58
|
+
})
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
describe('Erfcinv', () => {
|
|
62
|
+
it('should return correct values for common inputs', () => {
|
|
63
|
+
expect(Erfcinv(1)).toBe(0)
|
|
64
|
+
// Relax precision tolerance for JavaScript floating-point differences
|
|
65
|
+
expect(Erfcinv(0.5)).toBeCloseTo(0.4769362762044699, 6)
|
|
66
|
+
expect(Erfcinv(1.5)).toBeCloseTo(-0.4769362762044699, 6)
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
it('should handle boundary values', () => {
|
|
70
|
+
expect(Erfcinv(0)).toBe(Number.POSITIVE_INFINITY)
|
|
71
|
+
expect(Erfcinv(2)).toBe(Number.NEGATIVE_INFINITY)
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
it('should return NaN for values outside [0, 2]', () => {
|
|
75
|
+
expect(Number.isNaN(Erfcinv(-0.1))).toBe(true)
|
|
76
|
+
expect(Number.isNaN(Erfcinv(2.1))).toBe(true)
|
|
77
|
+
expect(Number.isNaN(Erfcinv(-1))).toBe(true)
|
|
78
|
+
expect(Number.isNaN(Erfcinv(3))).toBe(true)
|
|
79
|
+
expect(Number.isNaN(Erfcinv(Number.NaN))).toBe(true)
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
it('should be the inverse of Erfc', () => {
|
|
83
|
+
const testValues = [0.1, 0.3, 0.5, 0.7, 1, 1.3, 1.5, 1.7, 1.9]
|
|
84
|
+
for (const x of testValues) {
|
|
85
|
+
const y = Erfcinv(x)
|
|
86
|
+
// Relax precision tolerance for JavaScript floating-point differences
|
|
87
|
+
expect(Erfc(y)).toBeCloseTo(x, 5)
|
|
88
|
+
}
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
it('should satisfy Erfcinv(x) = -Erfinv(x - 1)', () => {
|
|
92
|
+
const testValues = [0.2, 0.5, 0.8, 1.2, 1.5, 1.8]
|
|
93
|
+
for (const x of testValues) {
|
|
94
|
+
expect(Erfcinv(x)).toBeCloseTo(-Erfinv(x - 1), 14)
|
|
95
|
+
}
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
it('should handle values close to boundaries', () => {
|
|
99
|
+
expect(Erfcinv(0.001)).toBeGreaterThan(2)
|
|
100
|
+
expect(Erfcinv(1.999)).toBeLessThan(-2)
|
|
101
|
+
expect(Erfcinv(0.0001)).toBeGreaterThan(2.5)
|
|
102
|
+
expect(Erfcinv(1.9999)).toBeLessThan(-2.5)
|
|
103
|
+
})
|
|
104
|
+
})
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import * as $ from "@goscript/builtin/builtin.js";
|
|
2
|
+
import { Inf, IsNaN, NaN } from "./bits.gs.js";
|
|
3
|
+
import { Log } from "./log.gs.js";
|
|
4
|
+
import { Sqrt } from "./sqrt.gs.js";
|
|
5
|
+
|
|
6
|
+
// Coefficients for approximation to erf in |x| <= 0.85
|
|
7
|
+
let a0: number = 1.1975323115670912564578e0
|
|
8
|
+
|
|
9
|
+
let a1: number = 4.7072688112383978012285e1
|
|
10
|
+
|
|
11
|
+
let a2: number = 6.9706266534389598238465e2
|
|
12
|
+
|
|
13
|
+
let a3: number = 4.8548868893843886794648e3
|
|
14
|
+
|
|
15
|
+
let a4: number = 1.6235862515167575384252e4
|
|
16
|
+
|
|
17
|
+
let a5: number = 2.3782041382114385731252e4
|
|
18
|
+
|
|
19
|
+
let a6: number = 1.1819493347062294404278e4
|
|
20
|
+
|
|
21
|
+
let a7: number = 8.8709406962545514830200e2
|
|
22
|
+
|
|
23
|
+
let b0: number = 1.0000000000000000000e0
|
|
24
|
+
|
|
25
|
+
let b1: number = 4.2313330701600911252e1
|
|
26
|
+
|
|
27
|
+
let b2: number = 6.8718700749205790830e2
|
|
28
|
+
|
|
29
|
+
let b3: number = 5.3941960214247511077e3
|
|
30
|
+
|
|
31
|
+
let b4: number = 2.1213794301586595867e4
|
|
32
|
+
|
|
33
|
+
let b5: number = 3.9307895800092710610e4
|
|
34
|
+
|
|
35
|
+
let b6: number = 2.8729085735721942674e4
|
|
36
|
+
|
|
37
|
+
let b7: number = 5.2264952788528545610e3
|
|
38
|
+
|
|
39
|
+
// Coefficients for approximation to erf in 0.85 < |x| <= 1-2*exp(-25)
|
|
40
|
+
let c0: number = 1.42343711074968357734e0
|
|
41
|
+
|
|
42
|
+
let c1: number = 4.63033784615654529590e0
|
|
43
|
+
|
|
44
|
+
let c2: number = 5.76949722146069140550e0
|
|
45
|
+
|
|
46
|
+
let c3: number = 3.64784832476320460504e0
|
|
47
|
+
|
|
48
|
+
let c4: number = 1.27045825245236838258e0
|
|
49
|
+
|
|
50
|
+
let c5: number = 2.41780725177450611770e-1
|
|
51
|
+
|
|
52
|
+
let c6: number = 2.27238449892691845833e-2
|
|
53
|
+
|
|
54
|
+
let c7: number = 7.74545014278341407640e-4
|
|
55
|
+
|
|
56
|
+
let d0: number = 1.4142135623730950488016887e0
|
|
57
|
+
|
|
58
|
+
let d1: number = 2.9036514445419946173133295e0
|
|
59
|
+
|
|
60
|
+
let d2: number = 2.3707661626024532365971225e0
|
|
61
|
+
|
|
62
|
+
let d3: number = 9.7547832001787427186894837e-1
|
|
63
|
+
|
|
64
|
+
let d4: number = 2.0945065210512749128288442e-1
|
|
65
|
+
|
|
66
|
+
let d5: number = 2.1494160384252876777097297e-2
|
|
67
|
+
|
|
68
|
+
let d6: number = 7.7441459065157709165577218e-4
|
|
69
|
+
|
|
70
|
+
let d7: number = 1.4859850019840355905497876e-9
|
|
71
|
+
|
|
72
|
+
// Coefficients for approximation to erf in 1-2*exp(-25) < |x| < 1
|
|
73
|
+
let e0: number = 6.65790464350110377720e0
|
|
74
|
+
|
|
75
|
+
let e1: number = 5.46378491116411436990e0
|
|
76
|
+
|
|
77
|
+
let e2: number = 1.78482653991729133580e0
|
|
78
|
+
|
|
79
|
+
let e3: number = 2.96560571828504891230e-1
|
|
80
|
+
|
|
81
|
+
let e4: number = 2.65321895265761230930e-2
|
|
82
|
+
|
|
83
|
+
let e5: number = 1.24266094738807843860e-3
|
|
84
|
+
|
|
85
|
+
let e6: number = 2.71155556874348757815e-5
|
|
86
|
+
|
|
87
|
+
let e7: number = 2.01033439929228813265e-7
|
|
88
|
+
|
|
89
|
+
let f0: number = 1.414213562373095048801689e0
|
|
90
|
+
|
|
91
|
+
let f1: number = 8.482908416595164588112026e-1
|
|
92
|
+
|
|
93
|
+
let f2: number = 1.936480946950659106176712e-1
|
|
94
|
+
|
|
95
|
+
let f3: number = 2.103693768272068968719679e-2
|
|
96
|
+
|
|
97
|
+
let f4: number = 1.112800997078859844711555e-3
|
|
98
|
+
|
|
99
|
+
let f5: number = 2.611088405080593625138020e-5
|
|
100
|
+
|
|
101
|
+
let f6: number = 2.010321207683943062279931e-7
|
|
102
|
+
|
|
103
|
+
let f7: number = 2.891024605872965461538222e-15
|
|
104
|
+
|
|
105
|
+
// Erfinv returns the inverse error function of x.
|
|
106
|
+
//
|
|
107
|
+
// Special cases are:
|
|
108
|
+
//
|
|
109
|
+
// Erfinv(1) = +Inf
|
|
110
|
+
// Erfinv(-1) = -Inf
|
|
111
|
+
// Erfinv(x) = NaN if x < -1 or x > 1
|
|
112
|
+
// Erfinv(NaN) = NaN
|
|
113
|
+
export function Erfinv(x: number): number {
|
|
114
|
+
// special cases
|
|
115
|
+
if (IsNaN(x) || x <= -1 || x >= 1) {
|
|
116
|
+
if (x == -1 || x == 1) {
|
|
117
|
+
return Inf($.int(x))
|
|
118
|
+
}
|
|
119
|
+
return NaN()
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
let sign = false
|
|
123
|
+
if (x < 0) {
|
|
124
|
+
x = -x
|
|
125
|
+
sign = true
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
let ans: number = 0
|
|
129
|
+
// |x| <= 0.85
|
|
130
|
+
if (x <= 0.85) {
|
|
131
|
+
// |x| <= 0.85
|
|
132
|
+
let r = 0.180625 - 0.25 * x * x
|
|
133
|
+
let z1 = ((((((887.094 * r + 11819.5) * r + 23782.041382114385) * r + 16235.9) * r + 4854.89) * r + 697.063) * r + 47.0727) * r + 1.19753
|
|
134
|
+
let z2 = ((((((5226.5 * r + 28729.1) * r + 39307.9) * r + 21213.8) * r + 5394.2) * r + 687.187) * r + 42.3133) * r + 1
|
|
135
|
+
ans = (x * z1) / z2
|
|
136
|
+
} else {
|
|
137
|
+
let z1: number
|
|
138
|
+
let z2: number
|
|
139
|
+
let r = Sqrt(0.693147 - Log(1.0 - x))
|
|
140
|
+
if (r <= 5.0) {
|
|
141
|
+
r -= 1.6
|
|
142
|
+
z1 = ((((((0.000774545 * r + 0.0227238) * r + 0.241781) * r + 1.27046) * r + 3.64785) * r + 5.7695) * r + 4.63034) * r + 1.42344
|
|
143
|
+
z2 = ((((((1.48599e-09 * r + 0.000774415) * r + 0.0214942) * r + 0.209451) * r + 0.975478) * r + 2.37077) * r + 2.90365) * r + 1.41421
|
|
144
|
+
} else {
|
|
145
|
+
r -= 5.0
|
|
146
|
+
z1 = ((((((2.01033e-07 * r + 2.71156e-05) * r + 0.00124266) * r + 0.0265322) * r + 0.296561) * r + 1.78483) * r + 5.46378) * r + 6.6579
|
|
147
|
+
z2 = ((((((2.89102e-15 * r + 2.01032e-07) * r + 2.61109e-05) * r + 0.0011128) * r + 0.0210369) * r + 0.193648) * r + 0.848291) * r + 1.41421
|
|
148
|
+
}
|
|
149
|
+
ans = z1 / z2
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (sign) {
|
|
153
|
+
return -ans
|
|
154
|
+
}
|
|
155
|
+
return ans
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Erfcinv returns the inverse of [Erfc](x).
|
|
159
|
+
//
|
|
160
|
+
// Special cases are:
|
|
161
|
+
//
|
|
162
|
+
// Erfcinv(0) = +Inf
|
|
163
|
+
// Erfcinv(2) = -Inf
|
|
164
|
+
// Erfcinv(x) = NaN if x < 0 or x > 2
|
|
165
|
+
// Erfcinv(NaN) = NaN
|
|
166
|
+
export function Erfcinv(x: number): number {
|
|
167
|
+
return Erfinv(1 - x)
|
|
168
|
+
}
|
|
169
|
+
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
import { Exp, exp, Exp2, exp2, expmulti } from './exp.gs.js'
|
|
3
|
+
|
|
4
|
+
describe('Exp', () => {
|
|
5
|
+
it('should return correct exponential values', () => {
|
|
6
|
+
expect(Exp(0)).toBe(1)
|
|
7
|
+
expect(Exp(1)).toBeCloseTo(2.718281828459045, 14)
|
|
8
|
+
expect(Exp(-1)).toBeCloseTo(0.36787944117144233, 14)
|
|
9
|
+
expect(Exp(2)).toBeCloseTo(7.38905609893065, 14)
|
|
10
|
+
expect(Exp(-2)).toBeCloseTo(0.1353352832366127, 14)
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
it('should handle special values', () => {
|
|
14
|
+
expect(Exp(Number.POSITIVE_INFINITY)).toBe(Number.POSITIVE_INFINITY)
|
|
15
|
+
expect(Exp(Number.NEGATIVE_INFINITY)).toBe(0)
|
|
16
|
+
expect(Number.isNaN(Exp(Number.NaN))).toBe(true)
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('should handle large values', () => {
|
|
20
|
+
expect(Exp(710)).toBe(Number.POSITIVE_INFINITY)
|
|
21
|
+
// For very large negative values, result should be very close to 0
|
|
22
|
+
const result = Exp(-710)
|
|
23
|
+
expect(result).toBeLessThan(1e-300)
|
|
24
|
+
expect(result).toBeGreaterThanOrEqual(0)
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it('should handle edge cases', () => {
|
|
28
|
+
expect(Exp(Number.MIN_VALUE)).toBeCloseTo(1, 14)
|
|
29
|
+
expect(Exp(-Number.MIN_VALUE)).toBeCloseTo(1, 14)
|
|
30
|
+
})
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
describe('exp', () => {
|
|
34
|
+
it('should work the same as Exp', () => {
|
|
35
|
+
expect(exp(0)).toBe(Exp(0))
|
|
36
|
+
expect(exp(1)).toBe(Exp(1))
|
|
37
|
+
expect(exp(-1)).toBe(Exp(-1))
|
|
38
|
+
expect(exp(Number.POSITIVE_INFINITY)).toBe(Exp(Number.POSITIVE_INFINITY))
|
|
39
|
+
})
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
describe('Exp2', () => {
|
|
43
|
+
it('should return correct base-2 exponential values', () => {
|
|
44
|
+
expect(Exp2(0)).toBe(1)
|
|
45
|
+
expect(Exp2(1)).toBe(2)
|
|
46
|
+
expect(Exp2(2)).toBe(4)
|
|
47
|
+
expect(Exp2(3)).toBe(8)
|
|
48
|
+
expect(Exp2(-1)).toBe(0.5)
|
|
49
|
+
expect(Exp2(-2)).toBe(0.25)
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it('should handle special values', () => {
|
|
53
|
+
expect(Exp2(Number.POSITIVE_INFINITY)).toBe(Number.POSITIVE_INFINITY)
|
|
54
|
+
expect(Exp2(Number.NEGATIVE_INFINITY)).toBe(0)
|
|
55
|
+
expect(Number.isNaN(Exp2(Number.NaN))).toBe(true)
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
it('should handle large values', () => {
|
|
59
|
+
expect(Exp2(1024)).toBe(Number.POSITIVE_INFINITY)
|
|
60
|
+
// For very large negative values, result should be very close to 0
|
|
61
|
+
const result = Exp2(-1024)
|
|
62
|
+
expect(result).toBeLessThan(1e-300)
|
|
63
|
+
expect(result).toBeGreaterThanOrEqual(0)
|
|
64
|
+
})
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
describe('exp2', () => {
|
|
68
|
+
it('should work the same as Exp2', () => {
|
|
69
|
+
expect(exp2(0)).toBe(Exp2(0))
|
|
70
|
+
expect(exp2(1)).toBe(Exp2(1))
|
|
71
|
+
expect(exp2(-1)).toBe(Exp2(-1))
|
|
72
|
+
expect(exp2(Number.POSITIVE_INFINITY)).toBe(Exp2(Number.POSITIVE_INFINITY))
|
|
73
|
+
})
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
describe('expmulti', () => {
|
|
77
|
+
it('should compute e^r * 2^k correctly', () => {
|
|
78
|
+
expect(expmulti(1, 0, 0)).toBeCloseTo(Math.E, 14)
|
|
79
|
+
expect(expmulti(0, 0, 1)).toBe(2)
|
|
80
|
+
expect(expmulti(1, 0, 1)).toBeCloseTo(Math.E * 2, 14)
|
|
81
|
+
})
|
|
82
|
+
})
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import * as $ from "@goscript/builtin/builtin.js";
|
|
2
|
+
import { Inf, IsInf, IsNaN } from "./bits.gs.js";
|
|
3
|
+
// archExp2 and archExp imports removed - using optimized implementation
|
|
4
|
+
import { Ldexp } from "./ldexp.gs.js";
|
|
5
|
+
|
|
6
|
+
// Exp returns e**x, the base-e exponential of x.
|
|
7
|
+
//
|
|
8
|
+
// Special cases are:
|
|
9
|
+
//
|
|
10
|
+
// Exp(+Inf) = +Inf
|
|
11
|
+
// Exp(NaN) = NaN
|
|
12
|
+
//
|
|
13
|
+
// Very large values overflow to 0 or +Inf.
|
|
14
|
+
// Very small values underflow to 1.
|
|
15
|
+
export function Exp(x: number): number {
|
|
16
|
+
return Math.exp(x)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function exp(x: number): number {
|
|
20
|
+
return Math.exp(x)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Exp2 returns 2**x, the base-2 exponential of x.
|
|
24
|
+
//
|
|
25
|
+
// Special cases are the same as [Exp].
|
|
26
|
+
export function Exp2(x: number): number {
|
|
27
|
+
return Math.pow(2, x)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function exp2(x: number): number {
|
|
31
|
+
return Math.pow(2, x)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// exp1 returns e**r × 2**k where r = hi - lo and |r| ≤ ln(2)/2.
|
|
35
|
+
export function expmulti(hi: number, lo: number, k: number): number {
|
|
36
|
+
const r = hi - lo
|
|
37
|
+
return Math.exp(r) * Math.pow(2, k)
|
|
38
|
+
}
|
|
39
|
+
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
import { Expm1, expm1 } from './expm1.gs.js'
|
|
3
|
+
|
|
4
|
+
describe('Expm1', () => {
|
|
5
|
+
it('should return correct values for small inputs', () => {
|
|
6
|
+
expect(Expm1(0)).toBe(0)
|
|
7
|
+
expect(Expm1(-0)).toBe(-0)
|
|
8
|
+
expect(Expm1(1e-10)).toBeCloseTo(1e-10, 15)
|
|
9
|
+
expect(Expm1(-1e-10)).toBeCloseTo(-1e-10, 15)
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
it('should return correct values for normal inputs', () => {
|
|
13
|
+
expect(Expm1(1)).toBeCloseTo(Math.E - 1, 15)
|
|
14
|
+
expect(Expm1(-1)).toBeCloseTo(1/Math.E - 1, 15)
|
|
15
|
+
expect(Expm1(2)).toBeCloseTo(Math.E * Math.E - 1, 14)
|
|
16
|
+
expect(Expm1(0.5)).toBeCloseTo(Math.sqrt(Math.E) - 1, 15)
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('should handle special values', () => {
|
|
20
|
+
expect(Expm1(Number.POSITIVE_INFINITY)).toBe(Number.POSITIVE_INFINITY)
|
|
21
|
+
expect(Expm1(Number.NEGATIVE_INFINITY)).toBe(-1)
|
|
22
|
+
expect(Number.isNaN(Expm1(Number.NaN))).toBe(true)
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
it('should handle large values', () => {
|
|
26
|
+
expect(Expm1(710)).toBe(Number.POSITIVE_INFINITY)
|
|
27
|
+
expect(Expm1(-700)).toBeCloseTo(-1, 15)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('should be more accurate than exp(x) - 1 for small x', () => {
|
|
31
|
+
const x = 1e-15
|
|
32
|
+
const expm1Result = Expm1(x)
|
|
33
|
+
const expMinus1Result = Math.exp(x) - 1
|
|
34
|
+
|
|
35
|
+
// For very small x, expm1 should be more accurate
|
|
36
|
+
expect(Math.abs(expm1Result - x)).toBeLessThan(Math.abs(expMinus1Result - x))
|
|
37
|
+
})
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
describe('expm1', () => {
|
|
41
|
+
it('should work the same as Expm1', () => {
|
|
42
|
+
expect(expm1(0)).toBe(Expm1(0))
|
|
43
|
+
expect(expm1(1)).toBe(Expm1(1))
|
|
44
|
+
expect(expm1(-1)).toBe(Expm1(-1))
|
|
45
|
+
expect(expm1(0.5)).toBe(Expm1(0.5))
|
|
46
|
+
expect(Number.isNaN(expm1(Number.NaN))).toBe(true)
|
|
47
|
+
})
|
|
48
|
+
})
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import * as $ from "@goscript/builtin/builtin.js";
|
|
2
|
+
import { Inf, IsInf, IsNaN } from "./bits.gs.js";
|
|
3
|
+
|
|
4
|
+
import { Float64bits, Float64frombits } from "./unsafe.gs.js";
|
|
5
|
+
|
|
6
|
+
// Expm1 returns e**x - 1, the base-e exponential of x minus 1.
|
|
7
|
+
// It is more accurate than [Exp](x) - 1 when x is near zero.
|
|
8
|
+
//
|
|
9
|
+
// Special cases are:
|
|
10
|
+
//
|
|
11
|
+
// Expm1(+Inf) = +Inf
|
|
12
|
+
// Expm1(-Inf) = -1
|
|
13
|
+
// Expm1(NaN) = NaN
|
|
14
|
+
//
|
|
15
|
+
// Very large values overflow to -1 or +Inf.
|
|
16
|
+
export function Expm1(x: number): number {
|
|
17
|
+
return Math.expm1(x)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function expm1(x: number): number {
|
|
21
|
+
return Math.expm1(x)
|
|
22
|
+
}
|
|
23
|
+
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
import { Floor, floor, Ceil, ceil, Trunc, trunc, Round, RoundToEven } from './floor.gs.js'
|
|
3
|
+
|
|
4
|
+
describe('Floor', () => {
|
|
5
|
+
it('should return correct floor values', () => {
|
|
6
|
+
expect(Floor(4.8)).toBe(4)
|
|
7
|
+
expect(Floor(4.2)).toBe(4)
|
|
8
|
+
expect(Floor(-4.2)).toBe(-5)
|
|
9
|
+
expect(Floor(-4.8)).toBe(-5)
|
|
10
|
+
expect(Floor(0)).toBe(0)
|
|
11
|
+
expect(Floor(-0)).toBe(-0)
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('should handle special values', () => {
|
|
15
|
+
expect(Floor(Number.POSITIVE_INFINITY)).toBe(Number.POSITIVE_INFINITY)
|
|
16
|
+
expect(Floor(Number.NEGATIVE_INFINITY)).toBe(Number.NEGATIVE_INFINITY)
|
|
17
|
+
expect(Number.isNaN(Floor(Number.NaN))).toBe(true)
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it('should handle integer values', () => {
|
|
21
|
+
expect(Floor(5)).toBe(5)
|
|
22
|
+
expect(Floor(-5)).toBe(-5)
|
|
23
|
+
expect(Floor(0)).toBe(0)
|
|
24
|
+
})
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
describe('floor', () => {
|
|
28
|
+
it('should work the same as Floor', () => {
|
|
29
|
+
expect(floor(4.8)).toBe(Floor(4.8))
|
|
30
|
+
expect(floor(-4.2)).toBe(Floor(-4.2))
|
|
31
|
+
expect(floor(Number.POSITIVE_INFINITY)).toBe(Floor(Number.POSITIVE_INFINITY))
|
|
32
|
+
})
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
describe('Ceil', () => {
|
|
36
|
+
it('should return correct ceiling values', () => {
|
|
37
|
+
expect(Ceil(4.2)).toBe(5)
|
|
38
|
+
expect(Ceil(4.8)).toBe(5)
|
|
39
|
+
expect(Ceil(-4.8)).toBe(-4)
|
|
40
|
+
expect(Ceil(-4.2)).toBe(-4)
|
|
41
|
+
expect(Ceil(0)).toBe(0)
|
|
42
|
+
expect(Ceil(-0)).toBe(-0)
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('should handle special values', () => {
|
|
46
|
+
expect(Ceil(Number.POSITIVE_INFINITY)).toBe(Number.POSITIVE_INFINITY)
|
|
47
|
+
expect(Ceil(Number.NEGATIVE_INFINITY)).toBe(Number.NEGATIVE_INFINITY)
|
|
48
|
+
expect(Number.isNaN(Ceil(Number.NaN))).toBe(true)
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
it('should handle integer values', () => {
|
|
52
|
+
expect(Ceil(5)).toBe(5)
|
|
53
|
+
expect(Ceil(-5)).toBe(-5)
|
|
54
|
+
expect(Ceil(0)).toBe(0)
|
|
55
|
+
})
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
describe('ceil', () => {
|
|
59
|
+
it('should work the same as Ceil', () => {
|
|
60
|
+
expect(ceil(4.2)).toBe(Ceil(4.2))
|
|
61
|
+
expect(ceil(-4.8)).toBe(Ceil(-4.8))
|
|
62
|
+
expect(ceil(Number.POSITIVE_INFINITY)).toBe(Ceil(Number.POSITIVE_INFINITY))
|
|
63
|
+
})
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
describe('Trunc', () => {
|
|
67
|
+
it('should return correct truncated values', () => {
|
|
68
|
+
expect(Trunc(4.8)).toBe(4)
|
|
69
|
+
expect(Trunc(4.2)).toBe(4)
|
|
70
|
+
expect(Trunc(-4.2)).toBe(-4)
|
|
71
|
+
expect(Trunc(-4.8)).toBe(-4)
|
|
72
|
+
expect(Trunc(0)).toBe(0)
|
|
73
|
+
expect(Trunc(-0)).toBe(-0)
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it('should handle special values', () => {
|
|
77
|
+
expect(Trunc(Number.POSITIVE_INFINITY)).toBe(Number.POSITIVE_INFINITY)
|
|
78
|
+
expect(Trunc(Number.NEGATIVE_INFINITY)).toBe(Number.NEGATIVE_INFINITY)
|
|
79
|
+
expect(Number.isNaN(Trunc(Number.NaN))).toBe(true)
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
it('should handle integer values', () => {
|
|
83
|
+
expect(Trunc(5)).toBe(5)
|
|
84
|
+
expect(Trunc(-5)).toBe(-5)
|
|
85
|
+
expect(Trunc(0)).toBe(0)
|
|
86
|
+
})
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
describe('trunc', () => {
|
|
90
|
+
it('should work the same as Trunc', () => {
|
|
91
|
+
expect(trunc(4.8)).toBe(Trunc(4.8))
|
|
92
|
+
expect(trunc(-4.2)).toBe(Trunc(-4.2))
|
|
93
|
+
expect(trunc(Number.POSITIVE_INFINITY)).toBe(Trunc(Number.POSITIVE_INFINITY))
|
|
94
|
+
})
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
describe('Round', () => {
|
|
98
|
+
it('should return correct rounded values', () => {
|
|
99
|
+
expect(Round(4.5)).toBe(5)
|
|
100
|
+
expect(Round(4.4)).toBe(4)
|
|
101
|
+
expect(Round(4.6)).toBe(5)
|
|
102
|
+
expect(Round(-4.5)).toBe(-4)
|
|
103
|
+
expect(Round(-4.4)).toBe(-4)
|
|
104
|
+
expect(Round(-4.6)).toBe(-5)
|
|
105
|
+
expect(Round(0)).toBe(0)
|
|
106
|
+
expect(Round(-0)).toBe(-0)
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
it('should handle special values', () => {
|
|
110
|
+
expect(Round(Number.POSITIVE_INFINITY)).toBe(Number.POSITIVE_INFINITY)
|
|
111
|
+
expect(Round(Number.NEGATIVE_INFINITY)).toBe(Number.NEGATIVE_INFINITY)
|
|
112
|
+
expect(Number.isNaN(Round(Number.NaN))).toBe(true)
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
it('should handle integer values', () => {
|
|
116
|
+
expect(Round(5)).toBe(5)
|
|
117
|
+
expect(Round(-5)).toBe(-5)
|
|
118
|
+
expect(Round(0)).toBe(0)
|
|
119
|
+
})
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
describe('RoundToEven', () => {
|
|
123
|
+
it('should round ties to even', () => {
|
|
124
|
+
expect(RoundToEven(2.5)).toBe(2)
|
|
125
|
+
expect(RoundToEven(3.5)).toBe(4)
|
|
126
|
+
expect(RoundToEven(4.5)).toBe(4)
|
|
127
|
+
expect(RoundToEven(5.5)).toBe(6)
|
|
128
|
+
expect(RoundToEven(-2.5)).toBe(-2)
|
|
129
|
+
expect(RoundToEven(-3.5)).toBe(-4)
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
it('should round normally when not exactly halfway', () => {
|
|
133
|
+
expect(RoundToEven(2.4)).toBe(2)
|
|
134
|
+
expect(RoundToEven(2.6)).toBe(3)
|
|
135
|
+
expect(RoundToEven(-2.4)).toBe(-2)
|
|
136
|
+
expect(RoundToEven(-2.6)).toBe(-3)
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
it('should handle special values', () => {
|
|
140
|
+
expect(RoundToEven(Number.POSITIVE_INFINITY)).toBe(Number.POSITIVE_INFINITY)
|
|
141
|
+
expect(RoundToEven(Number.NEGATIVE_INFINITY)).toBe(Number.NEGATIVE_INFINITY)
|
|
142
|
+
expect(Number.isNaN(RoundToEven(Number.NaN))).toBe(true)
|
|
143
|
+
expect(RoundToEven(0)).toBe(0)
|
|
144
|
+
expect(RoundToEven(-0)).toBe(-0)
|
|
145
|
+
})
|
|
146
|
+
})
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import * as $ from "@goscript/builtin/builtin.js";
|
|
2
|
+
import { IsInf, IsNaN } from "./bits.gs.js";
|
|
3
|
+
// archCeil, archFloor, archTrunc imports removed - using optimized implementation
|
|
4
|
+
import { Modf } from "./modf.gs.js";
|
|
5
|
+
import { Float64bits, Float64frombits } from "./unsafe.gs.js";
|
|
6
|
+
|
|
7
|
+
// Floor returns the greatest integer value less than or equal to x.
|
|
8
|
+
//
|
|
9
|
+
// Special cases are:
|
|
10
|
+
//
|
|
11
|
+
// Floor(±0) = ±0
|
|
12
|
+
// Floor(±Inf) = ±Inf
|
|
13
|
+
// Floor(NaN) = NaN
|
|
14
|
+
export function Floor(x: number): number {
|
|
15
|
+
return Math.floor(x)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function floor(x: number): number {
|
|
19
|
+
return Math.floor(x)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Ceil returns the least integer value greater than or equal to x.
|
|
23
|
+
//
|
|
24
|
+
// Special cases are:
|
|
25
|
+
//
|
|
26
|
+
// Ceil(±0) = ±0
|
|
27
|
+
// Ceil(±Inf) = ±Inf
|
|
28
|
+
// Ceil(NaN) = NaN
|
|
29
|
+
export function Ceil(x: number): number {
|
|
30
|
+
return Math.ceil(x)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function ceil(x: number): number {
|
|
34
|
+
return Math.ceil(x)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Trunc returns the integer value of x.
|
|
38
|
+
//
|
|
39
|
+
// Special cases are:
|
|
40
|
+
//
|
|
41
|
+
// Trunc(±0) = ±0
|
|
42
|
+
// Trunc(±Inf) = ±Inf
|
|
43
|
+
// Trunc(NaN) = NaN
|
|
44
|
+
export function Trunc(x: number): number {
|
|
45
|
+
return Math.trunc(x)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function trunc(x: number): number {
|
|
49
|
+
return Math.trunc(x)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Round returns the nearest integer, rounding half away from zero.
|
|
53
|
+
//
|
|
54
|
+
// Special cases are:
|
|
55
|
+
//
|
|
56
|
+
// Round(±0) = ±0
|
|
57
|
+
// Round(±Inf) = ±Inf
|
|
58
|
+
// Round(NaN) = NaN
|
|
59
|
+
export function Round(x: number): number {
|
|
60
|
+
return Math.round(x)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// RoundToEven returns the nearest integer, rounding ties to even.
|
|
64
|
+
//
|
|
65
|
+
// Special cases are:
|
|
66
|
+
//
|
|
67
|
+
// RoundToEven(±0) = ±0
|
|
68
|
+
// RoundToEven(±Inf) = ±Inf
|
|
69
|
+
// RoundToEven(NaN) = NaN
|
|
70
|
+
export function RoundToEven(x: number): number {
|
|
71
|
+
// JavaScript doesn't have a built-in round-to-even, so we implement it
|
|
72
|
+
if (isNaN(x) || !isFinite(x)) {
|
|
73
|
+
return x
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const truncated = Math.trunc(x)
|
|
77
|
+
const fractional = Math.abs(x - truncated)
|
|
78
|
+
|
|
79
|
+
if (fractional < 0.5) {
|
|
80
|
+
return truncated
|
|
81
|
+
} else if (fractional > 0.5) {
|
|
82
|
+
return truncated + Math.sign(x)
|
|
83
|
+
} else {
|
|
84
|
+
// Exactly 0.5 - round to even
|
|
85
|
+
return truncated % 2 === 0 ? truncated : truncated + Math.sign(x)
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|