Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Precision #191

Merged
merged 27 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading