math

package
v1.2.23 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 22, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Index

Constants

This section is empty.

Variables

View Source
var Infinity = InfInt{big.Int{}, infinity}

Infinity represents plain infinity

View Source
var NegInfinity = InfInt{big.Int{}, negativeInfinity}

NegInfinity represents negative infinity

View Source
var PosInfinity = InfInt{big.Int{}, positiveInfinity}

PosInfinity represents positive infinity

Functions

func NegPow2

func NegPow2(n uint) *big.Int

NegPow2 computes minus two reaised to a given power (i.e. -2^n)

func NextPowerOfTwo added in v1.2.21

func NextPowerOfTwo(n uint) uint

NextPowerOfTwo returns the smallest power of two greater than or equal to n, treating zero as one.

func Pow2

func Pow2(n uint) *big.Int

Pow2 computes two raised to a given power (i.e. 2^n)

func PowUint64

func PowUint64(base uint64, exp uint64) uint64

PowUint64 raises a given base raised to a given power.

func Sum

func Sum[T uint8 | uint16 | uint32 | uint64 | uint | int8 | int16 | int32 | int64 | int](items ...T) T

Sum an array of numbers

Types

type InfInt

type InfInt struct {
	// contains filtered or unexported fields
}

InfInt represents an unbound (i.e. big) integer value which can, additionally, be either negative infinity, positive infinity or just infinity (i.e. which covers all negative and positive values).

func (*InfInt) Add

func (p *InfInt) Add(other InfInt) InfInt

Add two (potentially infinite) integers together.

func (*InfInt) Cmp

func (p *InfInt) Cmp(o InfInt) int

Cmp performs a comparison of two (potentially infinite) integer values. This will panic if either value is plain infinity.

func (*InfInt) CmpInt

func (p *InfInt) CmpInt(other big.Int) int

CmpInt compares a potentially infinite integer value against a finite integer value. This will panic if the first value is plain infinity.

func (*InfInt) IntVal

func (p *InfInt) IntVal() big.Int

IntVal converts a potentially infinite integer into a finite value. This will panic if this value is an infinity.

func (*InfInt) IsNotAnInfinity

func (p *InfInt) IsNotAnInfinity() bool

IsNotAnInfinity returns true if this represents a finite integer value.

func (*InfInt) Max

func (p *InfInt) Max(o InfInt) InfInt

Max determines the greatest of two values. Note the semantics here are odd, as the maximum of plain infinity and anything is positive infinity!

func (*InfInt) Min

func (p *InfInt) Min(o InfInt) InfInt

Min determines the least of two values. Note the semantics here are odd, as the minimum of plain infinity and anything is negative infinity!

func (*InfInt) Mul

func (p *InfInt) Mul(o InfInt) InfInt

Mul multiplies a (potentially infinite) value against this (potentially infinite) value. If either operand is an infinity, then some kind of infinity is always returned.

func (*InfInt) Negate

func (p *InfInt) Negate() InfInt

Negate this (potentially infinite) integer.

func (*InfInt) Set

func (p *InfInt) Set(other InfInt)

Set this to match some (potentially infinite) integer. Observe this will clone the underlying big integer if the value is finite.

func (*InfInt) SetInt

func (p *InfInt) SetInt(other big.Int)

SetInt sets this to match a big integer. Observe this will clone the underlying big integer.

func (*InfInt) Sign

func (p *InfInt) Sign() int

Sign returns an indication whether this value is negative, zero or positive.

func (*InfInt) String

func (p *InfInt) String() string

func (*InfInt) Sub

func (p *InfInt) Sub(other InfInt) InfInt

Sub subtracts a (potentially infinite) value from this (potentially infinite) value.

type Interval

type Interval struct {
	// contains filtered or unexported fields
}

Interval provides a discrete range of integers, such as 0..1, 1..18, etc. An interval can be used to approximate the possible values that a given expression could evaluate to. An interval can additionally represent three different forms of infinity: negative infinity, positive infinity and plain infinity. The latter contains both negative and positive infinities. For more information on this system, see the following paper:

Integer Range Analysis for Whiley on Embedded Systems, David J. Pearce. In Proceedings of the IEEE/IFIP Workshop on Software Technologies for Future Embedded and Ubiquitous Systems (SEUS), pages 26--33, 2015.

INFINITY represents the interval which encloses all other intervals.

func NewInterval

func NewInterval(lower big.Int, upper big.Int) Interval

NewInterval creates an interval representing a given range.

func NewInterval64

func NewInterval64(lower int64, upper int64) Interval

NewInterval64 creates an interval representing a given range.

func (*Interval) Add

func (p *Interval) Add(q Interval)

Add two intervals together

func (*Interval) BitWidth

func (p *Interval) BitWidth() (width uint, signed bool)

BitWidth returns the minimum number of bits required to store all elements in this interval. Observe that, if the interval can contain negative numbers then it is considered to be "signed", and the bitwidth returned the maximum of either the positive or negative sides. Note: this method will panic if called with the infinite interval.

func (*Interval) Constant

func (p *Interval) Constant() *big.Int

Constant checks whether this interval represents a constant value and, if so, returns it. Otherwise, it returns nil.

func (*Interval) Contains

func (p *Interval) Contains(val big.Int) bool

Contains checks whether a given value is contained with this interval

func (*Interval) Exp

func (p *Interval) Exp(pow uint)

Exp raises this interval to a fix exponent.

func (*Interval) Insert

func (p *Interval) Insert(val Interval)

Insert a given value into this interval

func (*Interval) IsFinite

func (p *Interval) IsFinite() bool

IsFinite determines whether or not this interval represents an a finite value (i.e. not an infinity).

func (*Interval) IsInfinite

func (p *Interval) IsInfinite() bool

IsInfinite determines whether or not this interval represents an infinity.

func (*Interval) MaxIntValue

func (p *Interval) MaxIntValue() big.Int

MaxIntValue returns the maximum value that this interval includes. Note this will panic if the interval is infinite.

func (*Interval) MaxValue

func (p *Interval) MaxValue() InfInt

MaxValue returns the maximum value that this interval includes.

func (*Interval) MinIntValue

func (p *Interval) MinIntValue() big.Int

MinIntValue returns the minimum value that this interval includes. Note this will panic if the interval is infinite.

func (*Interval) MinValue

func (p *Interval) MinValue() InfInt

MinValue returns the minimum value that this interval includes.

func (*Interval) Mul

func (p *Interval) Mul(q Interval)

Mul multiplies this interval by another.

func (*Interval) Set

func (p *Interval) Set(val Interval)

Set assigns a given value to this interval. Note: this method will panic if called with the infinite interval.

func (*Interval) String

func (p *Interval) String() string

func (*Interval) Sub

func (p *Interval) Sub(q Interval)

Sub subtracts another interval from this.

func (*Interval) Union

func (p *Interval) Union(other Interval) Interval

Union returns the set union of two intervals.

func (*Interval) Within

func (p *Interval) Within(val Interval) bool

Within checks whether this interval is contained within the given bounds.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL