Skip to content

Conversation

@holiman
Copy link
Owner

@holiman holiman commented Mar 23, 2023

Ref: #123

This PR implements

// Float64 returns the float64 value nearest to x.
//
// Note: The `big.Float` version of `Float64` also returns an 'Accuracy', indicating
// whether the value was too small or too large to be represented by a
// `float64`. However, the `uint256` type is unable to represent values
// out of scope (|x| < math.SmallestNonzeroFloat64 or |x| > math.MaxFloat64),
// therefore this method does not return any accuracy.
//
// OBS! Currently, this method is not optimized: but uses big.Int and big.Float.
func (z *Int) Float64() float64

Implementing Float64 makes it possible to use e.g. the logarithm functions in the math package.

Todo:

  • Implement native to-float support without using package big

@codecov
Copy link

codecov bot commented Mar 23, 2023

Codecov Report

Merging #132 (fd5dfa8) into master (29b48c8) will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##            master      #132   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            5         5           
  Lines         1607      1615    +8     
=========================================
+ Hits          1607      1615    +8     

holiman added 3 commits March 23, 2023 09:16
name                       old time/op    new time/op    delta
Float64/Float64/uint256-8    67.0µs ± 6%    63.1µs ± 5%   -5.90%  (p=0.032 n=5+5)
Float64/Float64/big-8        55.5µs ± 7%    57.6µs ± 9%     ~     (p=0.222 n=5+5)

name                       old alloc/op   new alloc/op   delta
Float64/Float64/uint256-8    29.5kB ± 0%    28.5kB ± 0%   -3.47%  (p=0.008 n=5+5)
Float64/Float64/big-8        23.4kB ± 0%    23.4kB ± 0%     ~     (all equal)

name                       old allocs/op  new allocs/op  delta
Float64/Float64/uint256-8       701 ± 0%       573 ± 0%  -18.26%  (p=0.008 n=5+5)
Float64/Float64/big-8           510 ± 0%       510 ± 0%     ~     (all equal)
name                       old time/op    new time/op    delta
Float64/Float64/uint256-8    63.1µs ± 5%     3.6µs ±42%   -94.26%  (p=0.008 n=5+5)
Float64/Float64/big-8        57.6µs ± 9%    60.0µs ± 8%      ~     (p=0.421 n=5+5)

name                       old alloc/op   new alloc/op   delta
Float64/Float64/uint256-8    28.5kB ± 0%     0.0kB       -100.00%  (p=0.008 n=5+5)
Float64/Float64/big-8        23.4kB ± 0%    23.4kB ± 0%      ~     (all equal)

name                       old allocs/op  new allocs/op  delta
Float64/Float64/uint256-8       573 ± 0%         0       -100.00%  (p=0.008 n=5+5)
Float64/Float64/big-8           510 ± 0%       510 ± 0%      ~     (all equal)
@holiman
Copy link
Owner Author

holiman commented Mar 24, 2023

goos: linux
goarch: amd64
pkg: github.com/holiman/uint256
cpu: 12th Gen Intel(R) Core(TM) i7-1270P
BenchmarkFloat64/Float64/uint256-8                246834              4681 ns/op               0 B/op          0 allocs/op
BenchmarkFloat64/Float64/uint256-8                564375              4651 ns/op               0 B/op          0 allocs/op
BenchmarkFloat64/Float64/uint256-8                256735              4545 ns/op               0 B/op          0 allocs/op
BenchmarkFloat64/Float64/uint256-8                567476              2117 ns/op               0 B/op          0 allocs/op
BenchmarkFloat64/Float64/uint256-8                562767              2114 ns/op               0 B/op          0 allocs/op
BenchmarkFloat64/Float64/big-8                     23724             55516 ns/op           23424 B/op        510 allocs/op
BenchmarkFloat64/Float64/big-8                     18283             64730 ns/op           23424 B/op        510 allocs/op
BenchmarkFloat64/Float64/big-8                     18610             60523 ns/op           23424 B/op        510 allocs/op
BenchmarkFloat64/Float64/big-8                     19528             62700 ns/op           23424 B/op        510 allocs/op
BenchmarkFloat64/Float64/big-8                     19828             56352 ns/op           23424 B/op        510 allocs/op

@holiman holiman changed the title implement Float64 via package big + tests implement Float64() float64 Mar 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants