Skip to content

Commit

Permalink
Precision (#191)
Browse files Browse the repository at this point in the history
Fix precision on parsing.
Remove goveralls as it no longer provides accurate data.
  • Loading branch information
ohler55 authored Dec 26, 2024
1 parent dd8b637 commit 7994661
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 38 deletions.
27 changes: 13 additions & 14 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Go

on:
push:
branches: [ develop, master ]
#branches: [ develop, master ]
pull_request:
branches: [ develop ]

Expand All @@ -12,12 +12,12 @@ jobs:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: golangci-lint
uses: golangci/golangci-lint-action@v2
uses: golangci/golangci-lint-action@v4
with:
version: v1.51.2
version: v1.62.0

test:
strategy:
Expand All @@ -29,22 +29,21 @@ jobs:

runs-on: ${{ matrix.os }}-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: 1.19
go-version: 1.23

- name: Test
run: |
go test -covermode=count -coverprofile=cov.out ./...
go tool cover -func=cov.out
- name: Coverage
env:
GO111MODULE: off
COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
go get github.com/mattn/goveralls
goveralls -coverprofile=cov.out -service=github
# - name: Coverage
# env:
# COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# run: |
# go install github.com/mattn/goveralls@latest
# goveralls -coverprofile=cov.out -service=github
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# [![{}j](assets/ojg_comet.svg)](https://github.com/ohler55/ojg)

[![Build Status](https://github.com/ohler55/ojg/actions/workflows/CI.yml/badge.svg)](https://github.com/ohler55/ojg/actions)
[![Coverage Status](https://coveralls.io/repos/github/ohler55/ojg/badge.svg?branch=master)](https://coveralls.io/github/ohler55/ojg?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/ohler55/ojg)](https://goreportcard.com/report/github.com/ohler55/ojg)

Optimized JSON for Go is a high performance parser with a variety of
Expand Down
50 changes: 27 additions & 23 deletions gen/number.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package gen
import (
"encoding/json"
"math"
"runtime"
"strconv"

"github.com/ohler55/ojg"
Expand Down Expand Up @@ -136,32 +137,35 @@ func (n *Number) AsNum() (num any) {
num = i
}
default:
f := float64(n.I)
if 0 < n.Frac {
// Remove trailing zeros as they can cause precision loss due to
// the way go handles multiplication and division.
for 1 < n.Div && n.Frac%10 == 0 {
n.Frac /= 10
n.Div /= 10
if runtime.GOARCH == "arm64" {
f := float64(n.I)
if 0 < n.Frac {
// Remove trailing zeros as they can cause precision loss due to
// the way go or the hardware handles multiplication and division.
for 1 < n.Div && n.Frac%10 == 0 {
n.Frac /= 10
n.Div /= 10
}
// A simple division loses precision yet dividing 1.0 by the
// divisor and then multiplying the fraction seems to solve the
// issue on arm64 anyway.
f += float64(n.Frac) * (1.0 / float64(n.Div))
}
// A simple division loses precision yet dividing 1.0 by the
// divisor and then multiplying the fraction seems to solve the
// issue. As a guess it might have something to do with the
// operation being in base 2 with a special case for 1.0 divided
// by a number.
f += float64(n.Frac) * (1.0 / float64(n.Div))
}
if n.Neg {
f = -f
}
if 0 < n.Exp {
x := int(n.Exp)
if n.NegExp {
x = -x
if n.Neg {
f = -f
}
f *= math.Pow10(x)
if 0 < n.Exp {
x := int(n.Exp)
if n.NegExp {
x = -x
}
f *= math.Pow10(x)
}
num = f
} else {
n.FillBig()
num, _ = strconv.ParseFloat(string(n.BigBuf), 64)
}
num = f
}
return
}
Expand Down
1 change: 1 addition & 0 deletions gen/number_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func TestNumber(t *testing.T) {
{src: "123", value: 123},
{src: "-123", value: -123},
{src: "1.25", value: 1.25},
{src: "1.250", value: 1.25},
{src: "-1.25", value: -1.25},
{src: "1.25e3", value: 1.25e3},
{src: "-1.25e-1", value: -1.25e-1},
Expand Down

0 comments on commit 7994661

Please sign in to comment.