-
Notifications
You must be signed in to change notification settings - Fork 137
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
Introduce Sqrt
on NumberType
#3055
Open
darkdrag00nv2
wants to merge
5
commits into
onflow:master
Choose a base branch
from
darkdrag00nv2:math_funcs
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ee3813e
Introduce Sqrt on NumberType
darkdrag00nv2 90b485f
Define Math package and implement tests
darkdrag00nv2 370e29a
Add comment for the rounding mode
darkdrag00nv2 2beb223
Move calculation out of valueGetter
darkdrag00nv2 b2f799b
Add comment about 256 bit precision being sufficient
darkdrag00nv2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,7 @@ | |
"github.com/rivo/uniseg" | ||
"golang.org/x/text/unicode/norm" | ||
|
||
"github.com/onflow/cadence/fixedpoint" | ||
"github.com/onflow/cadence/runtime/ast" | ||
"github.com/onflow/cadence/runtime/common" | ||
"github.com/onflow/cadence/runtime/common/orderedmap" | ||
|
@@ -3405,6 +3406,7 @@ | |
Div(interpreter *Interpreter, other NumberValue, locationRange LocationRange) NumberValue | ||
SaturatingDiv(interpreter *Interpreter, other NumberValue, locationRange LocationRange) NumberValue | ||
ToBigEndianBytes() []byte | ||
Sqrt(*Interpreter, LocationRange) UFix64Value | ||
} | ||
|
||
func getNumberValueMember(interpreter *Interpreter, v NumberValue, name string, typ sema.Type, locationRange LocationRange) Value { | ||
|
@@ -3841,6 +3843,11 @@ | |
return v.Div(interpreter, other, locationRange) | ||
} | ||
|
||
func (v IntValue) Sqrt(interpreter *Interpreter, locationRange LocationRange) UFix64Value { | ||
val := v.ToBigInt(interpreter) | ||
return BigIntSqrt(interpreter, val, locationRange) | ||
} | ||
|
||
func (v IntValue) Less(interpreter *Interpreter, other ComparableValue, locationRange LocationRange) BoolValue { | ||
o, ok := other.(IntValue) | ||
if !ok { | ||
|
@@ -4498,6 +4505,12 @@ | |
return NewInt8Value(interpreter, valueGetter) | ||
} | ||
|
||
func (v Int8Value) Sqrt(interpreter *Interpreter, locationRange LocationRange) UFix64Value { | ||
val := new(big.Int) | ||
val.SetInt64(int64(v)) | ||
return BigIntSqrt(interpreter, val, locationRange) | ||
} | ||
|
||
func (v Int8Value) Less(interpreter *Interpreter, other ComparableValue, locationRange LocationRange) BoolValue { | ||
o, ok := other.(Int8Value) | ||
if !ok { | ||
|
@@ -5138,6 +5151,12 @@ | |
return NewInt16Value(interpreter, valueGetter) | ||
} | ||
|
||
func (v Int16Value) Sqrt(interpreter *Interpreter, locationRange LocationRange) UFix64Value { | ||
val := new(big.Int) | ||
val.SetInt64(int64(v)) | ||
return BigIntSqrt(interpreter, val, locationRange) | ||
} | ||
|
||
func (v Int16Value) Less(interpreter *Interpreter, other ComparableValue, locationRange LocationRange) BoolValue { | ||
o, ok := other.(Int16Value) | ||
if !ok { | ||
|
@@ -5781,6 +5800,12 @@ | |
return NewInt32Value(interpreter, valueGetter) | ||
} | ||
|
||
func (v Int32Value) Sqrt(interpreter *Interpreter, locationRange LocationRange) UFix64Value { | ||
val := new(big.Int) | ||
val.SetInt64(int64(v)) | ||
return BigIntSqrt(interpreter, val, locationRange) | ||
} | ||
|
||
func (v Int32Value) Less(interpreter *Interpreter, other ComparableValue, locationRange LocationRange) BoolValue { | ||
o, ok := other.(Int32Value) | ||
if !ok { | ||
|
@@ -6423,6 +6448,12 @@ | |
return NewInt64Value(interpreter, valueGetter) | ||
} | ||
|
||
func (v Int64Value) Sqrt(interpreter *Interpreter, locationRange LocationRange) UFix64Value { | ||
val := new(big.Int) | ||
val.SetInt64(int64(v)) | ||
return BigIntSqrt(interpreter, val, locationRange) | ||
} | ||
|
||
func (v Int64Value) Less(interpreter *Interpreter, other ComparableValue, locationRange LocationRange) BoolValue { | ||
o, ok := other.(Int64Value) | ||
if !ok { | ||
|
@@ -7120,6 +7151,11 @@ | |
return NewInt128ValueFromBigInt(interpreter, valueGetter) | ||
} | ||
|
||
func (v Int128Value) Sqrt(interpreter *Interpreter, locationRange LocationRange) UFix64Value { | ||
val := v.ToBigInt(interpreter) | ||
return BigIntSqrt(interpreter, val, locationRange) | ||
} | ||
|
||
func (v Int128Value) Less(interpreter *Interpreter, other ComparableValue, locationRange LocationRange) BoolValue { | ||
o, ok := other.(Int128Value) | ||
if !ok { | ||
|
@@ -7862,6 +7898,11 @@ | |
return NewInt256ValueFromBigInt(interpreter, valueGetter) | ||
} | ||
|
||
func (v Int256Value) Sqrt(interpreter *Interpreter, locationRange LocationRange) UFix64Value { | ||
val := v.ToBigInt(interpreter) | ||
return BigIntSqrt(interpreter, val, locationRange) | ||
} | ||
|
||
func (v Int256Value) Less(interpreter *Interpreter, other ComparableValue, locationRange LocationRange) BoolValue { | ||
o, ok := other.(Int256Value) | ||
if !ok { | ||
|
@@ -8514,6 +8555,11 @@ | |
return v.Div(interpreter, other, locationRange) | ||
} | ||
|
||
func (v UIntValue) Sqrt(interpreter *Interpreter, locationRange LocationRange) UFix64Value { | ||
val := v.ToBigInt(interpreter) | ||
return BigIntSqrt(interpreter, val, locationRange) | ||
} | ||
|
||
func (v UIntValue) Less(interpreter *Interpreter, other ComparableValue, locationRange LocationRange) BoolValue { | ||
o, ok := other.(UIntValue) | ||
if !ok { | ||
|
@@ -9078,6 +9124,12 @@ | |
return v.Div(interpreter, other, locationRange) | ||
} | ||
|
||
func (v UInt8Value) Sqrt(interpreter *Interpreter, locationRange LocationRange) UFix64Value { | ||
val := new(big.Int) | ||
val.SetUint64(uint64(v)) | ||
return BigIntSqrt(interpreter, val, locationRange) | ||
} | ||
|
||
func (v UInt8Value) Less(interpreter *Interpreter, other ComparableValue, locationRange LocationRange) BoolValue { | ||
o, ok := other.(UInt8Value) | ||
if !ok { | ||
|
@@ -9669,6 +9721,12 @@ | |
return v.Div(interpreter, other, locationRange) | ||
} | ||
|
||
func (v UInt16Value) Sqrt(interpreter *Interpreter, locationRange LocationRange) UFix64Value { | ||
val := new(big.Int) | ||
val.SetUint64(uint64(v)) | ||
return BigIntSqrt(interpreter, val, locationRange) | ||
} | ||
|
||
func (v UInt16Value) Less(interpreter *Interpreter, other ComparableValue, locationRange LocationRange) BoolValue { | ||
o, ok := other.(UInt16Value) | ||
if !ok { | ||
|
@@ -10211,6 +10269,12 @@ | |
return v.Div(interpreter, other, locationRange) | ||
} | ||
|
||
func (v UInt32Value) Sqrt(interpreter *Interpreter, locationRange LocationRange) UFix64Value { | ||
val := new(big.Int) | ||
val.SetUint64(uint64(v)) | ||
return BigIntSqrt(interpreter, val, locationRange) | ||
} | ||
|
||
func (v UInt32Value) Less(interpreter *Interpreter, other ComparableValue, locationRange LocationRange) BoolValue { | ||
o, ok := other.(UInt32Value) | ||
if !ok { | ||
|
@@ -10782,6 +10846,11 @@ | |
return v.Div(interpreter, other, locationRange) | ||
} | ||
|
||
func (v UInt64Value) Sqrt(interpreter *Interpreter, locationRange LocationRange) UFix64Value { | ||
val := v.ToBigInt(interpreter) | ||
return BigIntSqrt(interpreter, val, locationRange) | ||
} | ||
|
||
func (v UInt64Value) Less(interpreter *Interpreter, other ComparableValue, locationRange LocationRange) BoolValue { | ||
o, ok := other.(UInt64Value) | ||
if !ok { | ||
|
@@ -11400,6 +11469,11 @@ | |
return v.Div(interpreter, other, locationRange) | ||
} | ||
|
||
func (v UInt128Value) Sqrt(interpreter *Interpreter, locationRange LocationRange) UFix64Value { | ||
val := v.ToBigInt(interpreter) | ||
return BigIntSqrt(interpreter, val, locationRange) | ||
} | ||
|
||
func (v UInt128Value) Less(interpreter *Interpreter, other ComparableValue, locationRange LocationRange) BoolValue { | ||
o, ok := other.(UInt128Value) | ||
if !ok { | ||
|
@@ -12077,6 +12151,11 @@ | |
return v.Div(interpreter, other, locationRange) | ||
} | ||
|
||
func (v UInt256Value) Sqrt(interpreter *Interpreter, locationRange LocationRange) UFix64Value { | ||
val := v.ToBigInt(interpreter) | ||
return BigIntSqrt(interpreter, val, locationRange) | ||
} | ||
|
||
func (v UInt256Value) Less(interpreter *Interpreter, other ComparableValue, locationRange LocationRange) BoolValue { | ||
o, ok := other.(UInt256Value) | ||
if !ok { | ||
|
@@ -12581,6 +12660,12 @@ | |
panic(errors.NewUnreachableError()) | ||
} | ||
|
||
func (v Word8Value) Sqrt(interpreter *Interpreter, locationRange LocationRange) UFix64Value { | ||
val := new(big.Int) | ||
val.SetUint64(uint64(v)) | ||
return BigIntSqrt(interpreter, val, locationRange) | ||
} | ||
|
||
func (v Word8Value) Less(interpreter *Interpreter, other ComparableValue, locationRange LocationRange) BoolValue { | ||
o, ok := other.(Word8Value) | ||
if !ok { | ||
|
@@ -13017,6 +13102,12 @@ | |
panic(errors.NewUnreachableError()) | ||
} | ||
|
||
func (v Word16Value) Sqrt(interpreter *Interpreter, locationRange LocationRange) UFix64Value { | ||
val := new(big.Int) | ||
val.SetUint64(uint64(v)) | ||
return BigIntSqrt(interpreter, val, locationRange) | ||
} | ||
|
||
func (v Word16Value) Less(interpreter *Interpreter, other ComparableValue, locationRange LocationRange) BoolValue { | ||
o, ok := other.(Word16Value) | ||
if !ok { | ||
|
@@ -13456,6 +13547,12 @@ | |
panic(errors.NewUnreachableError()) | ||
} | ||
|
||
func (v Word32Value) Sqrt(interpreter *Interpreter, locationRange LocationRange) UFix64Value { | ||
val := new(big.Int) | ||
val.SetUint64(uint64(v)) | ||
return BigIntSqrt(interpreter, val, locationRange) | ||
} | ||
|
||
func (v Word32Value) Less(interpreter *Interpreter, other ComparableValue, locationRange LocationRange) BoolValue { | ||
o, ok := other.(Word32Value) | ||
if !ok { | ||
|
@@ -13921,6 +14018,11 @@ | |
panic(errors.NewUnreachableError()) | ||
} | ||
|
||
func (v Word64Value) Sqrt(interpreter *Interpreter, locationRange LocationRange) UFix64Value { | ||
val := v.ToBigInt(interpreter) | ||
return BigIntSqrt(interpreter, val, locationRange) | ||
} | ||
|
||
func (v Word64Value) Less(interpreter *Interpreter, other ComparableValue, locationRange LocationRange) BoolValue { | ||
o, ok := other.(Word64Value) | ||
if !ok { | ||
|
@@ -14438,6 +14540,11 @@ | |
panic(errors.NewUnreachableError()) | ||
} | ||
|
||
func (v Word128Value) Sqrt(interpreter *Interpreter, locationRange LocationRange) UFix64Value { | ||
val := v.ToBigInt(interpreter) | ||
return BigIntSqrt(interpreter, val, locationRange) | ||
} | ||
|
||
func (v Word128Value) Less(interpreter *Interpreter, other ComparableValue, locationRange LocationRange) BoolValue { | ||
o, ok := other.(Word128Value) | ||
if !ok { | ||
|
@@ -15018,6 +15125,11 @@ | |
panic(errors.NewUnreachableError()) | ||
} | ||
|
||
func (v Word256Value) Sqrt(interpreter *Interpreter, locationRange LocationRange) UFix64Value { | ||
val := v.ToBigInt(interpreter) | ||
return BigIntSqrt(interpreter, val, locationRange) | ||
} | ||
|
||
func (v Word256Value) Less(interpreter *Interpreter, other ComparableValue, locationRange LocationRange) BoolValue { | ||
o, ok := other.(Word256Value) | ||
if !ok { | ||
|
@@ -15705,6 +15817,13 @@ | |
) | ||
} | ||
|
||
func (v Fix64Value) Sqrt(interpreter *Interpreter, locationRange LocationRange) UFix64Value { | ||
val := new(big.Int).SetUint64(uint64(v)) | ||
sqrtWithFix64FactorSqrt := BigIntSqrt(interpreter, val, locationRange) | ||
sqrt := sqrtWithFix64FactorSqrt / fixedpoint.Fix64FactorSqrt | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Possible optimization: I think we can save this division if in the function |
||
return sqrt | ||
} | ||
|
||
func (v Fix64Value) Less(interpreter *Interpreter, other ComparableValue, locationRange LocationRange) BoolValue { | ||
o, ok := other.(Fix64Value) | ||
if !ok { | ||
|
@@ -16228,6 +16347,13 @@ | |
) | ||
} | ||
|
||
func (v UFix64Value) Sqrt(interpreter *Interpreter, locationRange LocationRange) UFix64Value { | ||
val := new(big.Int).SetUint64(uint64(v)) | ||
sqrtWithFix64FactorSqrt := BigIntSqrt(interpreter, val, locationRange) | ||
sqrt := sqrtWithFix64FactorSqrt / fixedpoint.Fix64FactorSqrt | ||
return sqrt | ||
} | ||
|
||
func (v UFix64Value) Less(interpreter *Interpreter, other ComparableValue, locationRange LocationRange) BoolValue { | ||
o, ok := other.(UFix64Value) | ||
if !ok { | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unlike the overflow case where the result can't fit into the target type, this case corresponds to an "undefined" case rather than an underflow. The square root being non defined for negative inputs (similar to the division by zero error where dividing by zero isn't mathematically defined).
There doesn't seem to be an "undefined" error available so I see why underflow was used, but I wanted to mention this.