github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/unit/angle.go (about)

     1  // Code generated by "go generate github.com/jingcheng-WU/gonum/unit”; DO NOT EDIT.
     2  
     3  // Copyright ©2014 The Gonum Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  package unit
     8  
     9  import (
    10  	"errors"
    11  	"fmt"
    12  	"math"
    13  	"unicode/utf8"
    14  )
    15  
    16  // Angle represents an angle in radians.
    17  type Angle float64
    18  
    19  const Rad Angle = 1
    20  
    21  // Unit converts the Angle to a *Unit.
    22  func (a Angle) Unit() *Unit {
    23  	return New(float64(a), Dimensions{
    24  		AngleDim: 1,
    25  	})
    26  }
    27  
    28  // Angle allows Angle to implement a Angleer interface.
    29  func (a Angle) Angle() Angle {
    30  	return a
    31  }
    32  
    33  // From converts the unit into the receiver. From returns an
    34  // error if there is a mismatch in dimension.
    35  func (a *Angle) From(u Uniter) error {
    36  	if !DimensionsMatch(u, Rad) {
    37  		*a = Angle(math.NaN())
    38  		return errors.New("unit: dimension mismatch")
    39  	}
    40  	*a = Angle(u.Unit().Value())
    41  	return nil
    42  }
    43  
    44  func (a Angle) Format(fs fmt.State, c rune) {
    45  	switch c {
    46  	case 'v':
    47  		if fs.Flag('#') {
    48  			fmt.Fprintf(fs, "%T(%v)", a, float64(a))
    49  			return
    50  		}
    51  		fallthrough
    52  	case 'e', 'E', 'f', 'F', 'g', 'G':
    53  		p, pOk := fs.Precision()
    54  		w, wOk := fs.Width()
    55  		const unit = " rad"
    56  		switch {
    57  		case pOk && wOk:
    58  			fmt.Fprintf(fs, "%*.*"+string(c), pos(w-utf8.RuneCount([]byte(unit))), p, float64(a))
    59  		case pOk:
    60  			fmt.Fprintf(fs, "%.*"+string(c), p, float64(a))
    61  		case wOk:
    62  			fmt.Fprintf(fs, "%*"+string(c), pos(w-utf8.RuneCount([]byte(unit))), float64(a))
    63  		default:
    64  			fmt.Fprintf(fs, "%"+string(c), float64(a))
    65  		}
    66  		fmt.Fprint(fs, unit)
    67  	default:
    68  		fmt.Fprintf(fs, "%%!%c(%T=%g rad)", c, a, float64(a))
    69  	}
    70  }