-
Notifications
You must be signed in to change notification settings - Fork 138
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
base: master
Are you sure you want to change the base?
Changes from all commits
ee3813e
90b485f
370e29a
2beb223
b2f799b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
"math/big" | ||
|
||
"github.com/onflow/cadence/runtime/errors" | ||
"github.com/onflow/cadence/runtime/sema" | ||
) | ||
|
||
func SignedBigIntToBigEndianBytes(bigInt *big.Int) []byte { | ||
|
@@ -140,3 +141,46 @@ | |
func BigEndianBytesToUnsignedBigInt(b []byte) *big.Int { | ||
return new(big.Int).SetBytes(b) | ||
} | ||
|
||
func BigIntSqrt(interpreter *Interpreter, value *big.Int, locationRange LocationRange) UFix64Value { | ||
if value.Sign() < 0 { | ||
panic(UnderflowError{ | ||
LocationRange: locationRange, | ||
}) | ||
} | ||
|
||
if value.Cmp(sema.MaxSquareIntegerBig) == 1 { | ||
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. When calling this check in the case of If I didn't miss anything and my comment makes sense, it may be better to have a specific function |
||
panic(OverflowError{ | ||
LocationRange: locationRange, | ||
}) | ||
} | ||
|
||
// Once we reach here, Cadence integer values are guaranteed to fit into | ||
// floating-point values with 256 bit precision _without_ truncation. | ||
// This is because of the above check with sema.MaxSquareIntegerBig. | ||
valueFloat := new(big.Float).SetPrec(256).SetInt(value) | ||
darkdrag00nv2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
res := new(big.Float).SetPrec(256).SetMode(big.ToZero).Sqrt(valueFloat) | ||
darkdrag00nv2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
res.Mul(res, new(big.Float).SetPrec(256).SetInt(sema.Fix64FactorBig)) | ||
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.
|
||
|
||
// Converting the result to a fixed-point number, we are conceptually converting it to an integer | ||
// IEEE 754 specifies different rounding modes https: //en.wikipedia.org/wiki/IEEE_754#Rounding_rules | ||
// We follow the "Rationale for International Standard -- Programming Languages -- C", Revision 5.10, April-2003: | ||
// > Section 6.3.1.5 Real floating types: | ||
// > When a finite value of real floating type is converted to an integer type other than Bool, | ||
// > the fractional part is discarded (i.e., the value is truncated toward zero). If the value | ||
// > of the integral part cannot be represented by the integer type, the behavior is undefined. | ||
// For details, see | ||
// https: //wiki.sei.cmu.edu/confluence/display/c/FLP34-C.+Ensure+that+floating-point+conversions+are+within+range+of+the+new+type | ||
resInt := new(big.Int) | ||
res.Int(resInt) | ||
if !resInt.IsUint64() { | ||
// We checked for overflow above, so we shouldn't hit this. | ||
panic(errors.NewUnreachableError()) | ||
} | ||
|
||
valueGetter := func() uint64 { | ||
return resInt.Uint64() | ||
} | ||
|
||
return NewUFix64Value(interpreter, valueGetter) | ||
} |
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 { | ||
|
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.