...

Source file src/github.com/cockroachdb/apd/v3/condition.go

Documentation: github.com/cockroachdb/apd/v3

     1  // Copyright 2016 The Cockroach Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    12  // implied. See the License for the specific language governing
    13  // permissions and limitations under the License.
    14  
    15  package apd
    16  
    17  import (
    18  	"errors"
    19  	"fmt"
    20  	"strings"
    21  )
    22  
    23  // Condition holds condition flags.
    24  type Condition uint32
    25  
    26  const (
    27  	// SystemOverflow is raised when an exponent is greater than MaxExponent.
    28  	SystemOverflow Condition = 1 << iota
    29  	// SystemUnderflow is raised when an exponent is less than MinExponent.
    30  	SystemUnderflow
    31  	// Overflow is raised when the exponent of a result is too large to be
    32  	// represented.
    33  	Overflow
    34  	// Underflow is raised when a result is both subnormal and inexact.
    35  	Underflow
    36  	// Inexact is raised when a result is not exact (one or more non-zero
    37  	// coefficient digits were discarded during rounding).
    38  	Inexact
    39  	// Subnormal is raised when a result is subnormal (its adjusted exponent is
    40  	// less than Emin), before any rounding.
    41  	Subnormal
    42  	// Rounded is raised when a result has been rounded (that is, some zero or
    43  	// non-zero coefficient digits were discarded).
    44  	Rounded
    45  	// DivisionUndefined is raised when both division operands are 0.
    46  	DivisionUndefined
    47  	// DivisionByZero is raised when a non-zero dividend is divided by zero.
    48  	DivisionByZero
    49  	// DivisionImpossible is raised when integer division cannot be exactly
    50  	// represented with the given precision.
    51  	DivisionImpossible
    52  	// InvalidOperation is raised when a result would be undefined or impossible.
    53  	InvalidOperation
    54  	// Clamped is raised when the exponent of a result has been altered or
    55  	// constrained in order to fit the constraints of the Decimal representation.
    56  	Clamped
    57  )
    58  
    59  // Any returns true if any flag is true.
    60  func (r Condition) Any() bool { return r != 0 }
    61  
    62  // SystemOverflow returns true if the SystemOverflow flag is set.
    63  func (r Condition) SystemOverflow() bool { return r&SystemOverflow != 0 }
    64  
    65  // SystemUnderflow returns true if the SystemUnderflow flag is set.
    66  func (r Condition) SystemUnderflow() bool { return r&SystemUnderflow != 0 }
    67  
    68  // Overflow returns true if the Overflow flag is set.
    69  func (r Condition) Overflow() bool { return r&Overflow != 0 }
    70  
    71  // Underflow returns true if the Underflow flag is set.
    72  func (r Condition) Underflow() bool { return r&Underflow != 0 }
    73  
    74  // Inexact returns true if the Inexact flag is set.
    75  func (r Condition) Inexact() bool { return r&Inexact != 0 }
    76  
    77  // Subnormal returns true if the Subnormal flag is set.
    78  func (r Condition) Subnormal() bool { return r&Subnormal != 0 }
    79  
    80  // Rounded returns true if the Rounded flag is set.
    81  func (r Condition) Rounded() bool { return r&Rounded != 0 }
    82  
    83  // DivisionUndefined returns true if the DivisionUndefined flag is set.
    84  func (r Condition) DivisionUndefined() bool { return r&DivisionUndefined != 0 }
    85  
    86  // DivisionByZero returns true if the DivisionByZero flag is set.
    87  func (r Condition) DivisionByZero() bool { return r&DivisionByZero != 0 }
    88  
    89  // DivisionImpossible returns true if the DivisionImpossible flag is set.
    90  func (r Condition) DivisionImpossible() bool { return r&DivisionImpossible != 0 }
    91  
    92  // InvalidOperation returns true if the InvalidOperation flag is set.
    93  func (r Condition) InvalidOperation() bool { return r&InvalidOperation != 0 }
    94  
    95  // Clamped returns true if the Clamped flag is set.
    96  func (r Condition) Clamped() bool { return r&Clamped != 0 }
    97  
    98  // GoError converts r to an error based on the given traps and returns
    99  // r. Traps are the conditions which will trigger an error result if the
   100  // corresponding Flag condition occurred.
   101  func (r Condition) GoError(traps Condition) (Condition, error) {
   102  	const (
   103  		systemErrors = SystemOverflow | SystemUnderflow
   104  	)
   105  	var err error
   106  	if r&systemErrors != 0 {
   107  		err = errors.New(errExponentOutOfRangeStr)
   108  	} else if t := r & traps; t != 0 {
   109  		err = errors.New(t.String())
   110  	}
   111  	return r, err
   112  }
   113  
   114  func (r Condition) String() string {
   115  	var names []string
   116  	for i := Condition(1); r != 0; i <<= 1 {
   117  		if r&i == 0 {
   118  			continue
   119  		}
   120  		r ^= i
   121  		var s string
   122  		switch i {
   123  		case SystemOverflow, SystemUnderflow:
   124  			continue
   125  		case Overflow:
   126  			s = "overflow"
   127  		case Underflow:
   128  			s = "underflow"
   129  		case Inexact:
   130  			s = "inexact"
   131  		case Subnormal:
   132  			s = "subnormal"
   133  		case Rounded:
   134  			s = "rounded"
   135  		case DivisionUndefined:
   136  			s = "division undefined"
   137  		case DivisionByZero:
   138  			s = "division by zero"
   139  		case DivisionImpossible:
   140  			s = "division impossible"
   141  		case InvalidOperation:
   142  			s = "invalid operation"
   143  		case Clamped:
   144  			s = "clamped"
   145  		default:
   146  			panic(fmt.Errorf("unknown condition %d", i))
   147  		}
   148  		names = append(names, s)
   149  	}
   150  	return strings.Join(names, ", ")
   151  }
   152  
   153  // negateOverflowFlags converts Overflow and SystemOverflow flags into their
   154  // equivalent Underflows.
   155  func (r Condition) negateOverflowFlags() Condition {
   156  	if r.Overflow() {
   157  		// Underflow always also means Subnormal. See GDA definition.
   158  		r |= Underflow | Subnormal
   159  		r &= ^Overflow
   160  	}
   161  	if r.SystemOverflow() {
   162  		r |= SystemUnderflow
   163  		r &= ^SystemOverflow
   164  	}
   165  	return r
   166  }
   167  

View as plain text