gonum.org/v1/gonum@v0.14.0/unit/voltage.go (about)

     1  // Code generated by "go generate gonum.org/v1/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  // Voltage represents a voltage in Volts.
    17  type Voltage float64
    18  
    19  const Volt Voltage = 1
    20  
    21  // Unit converts the Voltage to a *Unit.
    22  func (v Voltage) Unit() *Unit {
    23  	return New(float64(v), Dimensions{
    24  		CurrentDim: -1,
    25  		LengthDim:  2,
    26  		MassDim:    1,
    27  		TimeDim:    -3,
    28  	})
    29  }
    30  
    31  // Voltage allows Voltage to implement a Voltager interface.
    32  func (v Voltage) Voltage() Voltage {
    33  	return v
    34  }
    35  
    36  // From converts the unit into the receiver. From returns an
    37  // error if there is a mismatch in dimension.
    38  func (v *Voltage) From(u Uniter) error {
    39  	if !DimensionsMatch(u, Volt) {
    40  		*v = Voltage(math.NaN())
    41  		return errors.New("unit: dimension mismatch")
    42  	}
    43  	*v = Voltage(u.Unit().Value())
    44  	return nil
    45  }
    46  
    47  func (v Voltage) Format(fs fmt.State, c rune) {
    48  	switch c {
    49  	case 'v':
    50  		if fs.Flag('#') {
    51  			fmt.Fprintf(fs, "%T(%v)", v, float64(v))
    52  			return
    53  		}
    54  		fallthrough
    55  	case 'e', 'E', 'f', 'F', 'g', 'G':
    56  		p, pOk := fs.Precision()
    57  		w, wOk := fs.Width()
    58  		const unit = " V"
    59  		switch {
    60  		case pOk && wOk:
    61  			fmt.Fprintf(fs, "%*.*"+string(c), pos(w-utf8.RuneCount([]byte(unit))), p, float64(v))
    62  		case pOk:
    63  			fmt.Fprintf(fs, "%.*"+string(c), p, float64(v))
    64  		case wOk:
    65  			fmt.Fprintf(fs, "%*"+string(c), pos(w-utf8.RuneCount([]byte(unit))), float64(v))
    66  		default:
    67  			fmt.Fprintf(fs, "%"+string(c), float64(v))
    68  		}
    69  		fmt.Fprint(fs, unit)
    70  	default:
    71  		fmt.Fprintf(fs, "%%!%c(%T=%g V)", c, v, float64(v))
    72  	}
    73  }