...

Source file src/github.com/cockroachdb/apd/v3/error.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  // MakeErrDecimal creates a ErrDecimal with given context.
    18  func MakeErrDecimal(c *Context) ErrDecimal {
    19  	return ErrDecimal{
    20  		Ctx: c,
    21  	}
    22  }
    23  
    24  // ErrDecimal performs operations on decimals and collects errors during
    25  // operations. If an error is already set, the operation is skipped. Designed to
    26  // be used for many operations in a row, with a single error check at the end.
    27  type ErrDecimal struct {
    28  	err error
    29  	Ctx *Context
    30  	// Flags are the accumulated flags from operations.
    31  	Flags Condition
    32  }
    33  
    34  // Err returns the first error encountered or the context's trap error
    35  // if present.
    36  func (e *ErrDecimal) Err() error {
    37  	if e.err != nil {
    38  		return e.err
    39  	}
    40  	if e.Ctx != nil {
    41  		_, e.err = e.Ctx.goError(e.Flags)
    42  		return e.err
    43  	}
    44  	return nil
    45  }
    46  
    47  // update adjusts the ErrDecimal's state with the result of an operation.
    48  func (e *ErrDecimal) update(res Condition, err error) {
    49  	e.Flags |= res
    50  	e.err = err
    51  }
    52  
    53  // Abs performs e.Ctx.Abs(d, x) and returns d.
    54  func (e *ErrDecimal) Abs(d, x *Decimal) *Decimal {
    55  	if e.Err() != nil {
    56  		return d
    57  	}
    58  	e.update(e.Ctx.Abs(d, x))
    59  	return d
    60  }
    61  
    62  // Add performs e.Ctx.Add(d, x, y) and returns d.
    63  func (e *ErrDecimal) Add(d, x, y *Decimal) *Decimal {
    64  	if e.Err() != nil {
    65  		return d
    66  	}
    67  	e.update(e.Ctx.Add(d, x, y))
    68  	return d
    69  }
    70  
    71  // Ceil performs e.Ctx.Ceil(d, x) and returns d.
    72  func (e *ErrDecimal) Ceil(d, x *Decimal) *Decimal {
    73  	if e.Err() != nil {
    74  		return d
    75  	}
    76  	e.update(e.Ctx.Ceil(d, x))
    77  	return d
    78  }
    79  
    80  // Exp performs e.Ctx.Exp(d, x) and returns d.
    81  func (e *ErrDecimal) Exp(d, x *Decimal) *Decimal {
    82  	if e.Err() != nil {
    83  		return d
    84  	}
    85  	e.update(e.Ctx.Exp(d, x))
    86  	return d
    87  }
    88  
    89  // Floor performs e.Ctx.Floor(d, x) and returns d.
    90  func (e *ErrDecimal) Floor(d, x *Decimal) *Decimal {
    91  	if e.Err() != nil {
    92  		return d
    93  	}
    94  	e.update(e.Ctx.Floor(d, x))
    95  	return d
    96  }
    97  
    98  // Int64 returns 0 if err is set. Otherwise returns d.Int64().
    99  func (e *ErrDecimal) Int64(d *Decimal) int64 {
   100  	if e.Err() != nil {
   101  		return 0
   102  	}
   103  	var r int64
   104  	r, e.err = d.Int64()
   105  	return r
   106  }
   107  
   108  // Ln performs e.Ctx.Ln(d, x) and returns d.
   109  func (e *ErrDecimal) Ln(d, x *Decimal) *Decimal {
   110  	if e.Err() != nil {
   111  		return d
   112  	}
   113  	e.update(e.Ctx.Ln(d, x))
   114  	return d
   115  }
   116  
   117  // Log10 performs d.Log10(x) and returns d.
   118  func (e *ErrDecimal) Log10(d, x *Decimal) *Decimal {
   119  	if e.Err() != nil {
   120  		return d
   121  	}
   122  	e.update(e.Ctx.Log10(d, x))
   123  	return d
   124  }
   125  
   126  // Mul performs e.Ctx.Mul(d, x, y) and returns d.
   127  func (e *ErrDecimal) Mul(d, x, y *Decimal) *Decimal {
   128  	if e.Err() != nil {
   129  		return d
   130  	}
   131  	e.update(e.Ctx.Mul(d, x, y))
   132  	return d
   133  }
   134  
   135  // Neg performs e.Ctx.Neg(d, x) and returns d.
   136  func (e *ErrDecimal) Neg(d, x *Decimal) *Decimal {
   137  	if e.Err() != nil {
   138  		return d
   139  	}
   140  	e.update(e.Ctx.Neg(d, x))
   141  	return d
   142  }
   143  
   144  // Pow performs e.Ctx.Pow(d, x, y) and returns d.
   145  func (e *ErrDecimal) Pow(d, x, y *Decimal) *Decimal {
   146  	if e.Err() != nil {
   147  		return d
   148  	}
   149  	e.update(e.Ctx.Pow(d, x, y))
   150  	return d
   151  }
   152  
   153  // Quantize performs e.Ctx.Quantize(d, v, exp) and returns d.
   154  func (e *ErrDecimal) Quantize(d, v *Decimal, exp int32) *Decimal {
   155  	if e.Err() != nil {
   156  		return d
   157  	}
   158  	e.update(e.Ctx.Quantize(d, v, exp))
   159  	return d
   160  }
   161  
   162  // Quo performs e.Ctx.Quo(d, x, y) and returns d.
   163  func (e *ErrDecimal) Quo(d, x, y *Decimal) *Decimal {
   164  	if e.Err() != nil {
   165  		return d
   166  	}
   167  	e.update(e.Ctx.Quo(d, x, y))
   168  	return d
   169  }
   170  
   171  // QuoInteger performs e.Ctx.QuoInteger(d, x, y) and returns d.
   172  func (e *ErrDecimal) QuoInteger(d, x, y *Decimal) *Decimal {
   173  	if e.Err() != nil {
   174  		return d
   175  	}
   176  	e.update(e.Ctx.QuoInteger(d, x, y))
   177  	return d
   178  }
   179  
   180  // Reduce performs e.Ctx.Reduce(d, x) and returns the number of zeros removed
   181  // and d.
   182  func (e *ErrDecimal) Reduce(d, x *Decimal) (int, *Decimal) {
   183  	if e.Err() != nil {
   184  		return 0, d
   185  	}
   186  	n, res, err := e.Ctx.Reduce(d, x)
   187  	e.update(res, err)
   188  	return n, d
   189  }
   190  
   191  // Rem performs e.Ctx.Rem(d, x, y) and returns d.
   192  func (e *ErrDecimal) Rem(d, x, y *Decimal) *Decimal {
   193  	if e.Err() != nil {
   194  		return d
   195  	}
   196  	e.update(e.Ctx.Rem(d, x, y))
   197  	return d
   198  }
   199  
   200  // Round performs e.Ctx.Round(d, x) and returns d.
   201  func (e *ErrDecimal) Round(d, x *Decimal) *Decimal {
   202  	if e.Err() != nil {
   203  		return d
   204  	}
   205  	e.update(e.Ctx.Round(d, x))
   206  	return d
   207  }
   208  
   209  // Sqrt performs e.Ctx.Sqrt(d, x) and returns d.
   210  func (e *ErrDecimal) Sqrt(d, x *Decimal) *Decimal {
   211  	if e.Err() != nil {
   212  		return d
   213  	}
   214  	e.update(e.Ctx.Sqrt(d, x))
   215  	return d
   216  }
   217  
   218  // Sub performs e.Ctx.Sub(d, x, y) and returns d.
   219  func (e *ErrDecimal) Sub(d, x, y *Decimal) *Decimal {
   220  	if e.Err() != nil {
   221  		return d
   222  	}
   223  	e.update(e.Ctx.Sub(d, x, y))
   224  	return d
   225  }
   226  
   227  // RoundToIntegralValue performs e.Ctx.RoundToIntegralValue(d, x) and returns d.
   228  func (e *ErrDecimal) RoundToIntegralValue(d, x *Decimal) *Decimal {
   229  	if e.Err() != nil {
   230  		return d
   231  	}
   232  	e.update(e.Ctx.RoundToIntegralValue(d, x))
   233  	return d
   234  }
   235  
   236  // RoundToIntegralExact performs e.Ctx.RoundToIntegralExact(d, x) and returns d.
   237  func (e *ErrDecimal) RoundToIntegralExact(d, x *Decimal) *Decimal {
   238  	if e.Err() != nil {
   239  		return d
   240  	}
   241  	e.update(e.Ctx.RoundToIntegralExact(d, x))
   242  	return d
   243  }
   244  

View as plain text