Source File
math.go
Belonging Package
github.com/apache/arrow-go/v18/internal/utils
// Licensed to the Apache Software Foundation (ASF) under one// or more contributor license agreements. See the NOTICE file// distributed with this work for additional information// regarding copyright ownership. The ASF licenses this file// to you 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.package utilsimport ()func [ cmp.Ordered](, ) {if < {return}return}func [ cmp.Ordered](, ) {if > {return}return}// Add returns the sum of two integers while checking for [overflow].// It returns false (not ok) when the operation overflows.//// [overflow]: https://go.dev/ref/spec#Integer_overflowfunc [ constraints.Signed](, ) (, bool) {// Overflow occurs when a and b are too positive or too negative.// That is, when: (a > 0) && (b > 0) && (a > math.Max[T] - b)// or when: (a < 0) && (b < 0) && (a < math.Min[T] - b):= +// No overflow occurred if the result is larger exactly when b is positive.return , ( > ) == ( > 0)}const (sqrtMaxInt = 1<<((bits.UintSize>>1)-1) - 1sqrtMinInt = -1 << ((bits.UintSize >> 1) - 1))// Mul returns the product of two integers while checking for [overflow].// It returns false (not ok) when the operation overflows.//// [overflow]: https://go.dev/ref/spec#Integer_overflowfunc (, int) (int, bool) {// Avoid division by zero and calculate nothing when a or b is zero.if == 0 || == 0 {return 0, true}:= *// Overflow occurred if the result is positive when exactly one input// is negative.if > 0 == (( < 0) != ( < 0)) {return , false}// Overflow cannot occur when a or b is zero or one.// Overflow cannot occur when a and b are less positive than sqrt(MaxInt).// Overflow cannot occur when a and b are less negative than sqrt(MinInt).if (sqrtMinInt <= && <= sqrtMaxInt &&sqrtMinInt <= && <= sqrtMaxInt) || == 1 || == 1 {return , true}// Finally, no overflow occurred if division produces the input. This is// last because division can be expensive. Dividing by -1 can overflow,// but we returned early in that case above.return , (/ == )}const (sqrtMaxInt64 = math.MaxInt32sqrtMinInt64 = math.MinInt32)// Mul64 returns the product of two integers while checking for [overflow].// It returns false (not ok) when the operation overflows.//// [overflow]: https://go.dev/ref/spec#Integer_overflowfunc (, int64) (int64, bool) {// Avoid division by zero and calculate nothing when a or b is zero.if == 0 || == 0 {return 0, true}:= *// Overflow occurred if the result is positive when exactly one input// is negative.if > 0 == (( < 0) != ( < 0)) {return , false}// Overflow cannot occur when a or b is zero or one.// Overflow cannot occur when a and b are less positive than sqrt(MaxInt64).// Overflow cannot occur when a and b are less negative than sqrt(MinInt64).if (sqrtMinInt64 <= && <= sqrtMaxInt64 &&sqrtMinInt64 <= && <= sqrtMaxInt64) || == 1 || == 1 {return , true}// Finally, no overflow occurred if division produces the input. This is// last because division can be expensive. Dividing by -1 can overflow,// but we returned early in that case above.return , (/ == )}
![]() |
The pages are generated with Golds v0.8.2. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |