Skip to content

Commit

Permalink
fix isnormal and normals functions against subnormal numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Dec 14, 2022
1 parent c1a3ebd commit afc4c6c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
27 changes: 21 additions & 6 deletions cli/test.yaml
Expand Up @@ -4075,25 +4075,40 @@
- name: isnormal function
args:
- 'nan, infinite, -infinite, 9 | ., 1/. | isnormal'
- '0, 2.2250738585072011e-308, 2.2250738585072014e-308, 1, 1.7976931348623157e+308, nan, infinite, "", [], {} | isnormal'
input: 'null'
expected: |
false
false
true
true
true
false
false
false
false
false
true
true
- name: normals function
args:
- -c
- 'nan, infinite, -infinite, "", [], {}, 0, 9 | normals'
- '0, 2.2250738585072011e-308, 2.2250738585072014e-308, 1, 1.7976931348623157e+308, nan, infinite | ., -. | normals'
input: 'null'
expected: |
9
2.2250738585072014e-308
-2.2250738585072014e-308
1
-1
1.7976931348623157e+308
-1.7976931348623157e+308
- name: normals with nextafter function
args:
- '0, 2.2250738585072014e-308, 1.7976931348623157e+308, nan, infinite | nextafter(.; 0, infinite) | normals'
input: 'null'
expected: |
2.225073858507202e-308
1.7976931348623155e+308
1.7976931348623157e+308
- name: stringify nan and infinite
args:
Expand Down
7 changes: 5 additions & 2 deletions func.go
Expand Up @@ -1411,8 +1411,11 @@ func funcIsnan(v interface{}) interface{} {
}

func funcIsnormal(v interface{}) interface{} {
x, ok := toFloat(v)
return ok && !math.IsNaN(x) && !math.IsInf(x, 0) && x != 0.0
if v, ok := toFloat(v); ok {
e := math.Float64bits(v) & 0x7ff0000000000000 >> 52
return 0 < e && e < 0x7ff
}
return false
}

// An `allocator` creates new maps and slices, stores the allocated addresses.
Expand Down

0 comments on commit afc4c6c

Please sign in to comment.