github.com/gopherd/gonum@v0.0.4/unit/resistance.go (about) 1 // Code generated by "go generate github.com/gopherd/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 // Resistance represents an electrical resistance, impedance or reactance in Ohms. 17 type Resistance float64 18 19 const Ohm Resistance = 1 20 21 // Unit converts the Resistance to a *Unit. 22 func (r Resistance) Unit() *Unit { 23 return New(float64(r), Dimensions{ 24 CurrentDim: -2, 25 LengthDim: 2, 26 MassDim: 1, 27 TimeDim: -3, 28 }) 29 } 30 31 // Resistance allows Resistance to implement a Resistancer interface. 32 func (r Resistance) Resistance() Resistance { 33 return r 34 } 35 36 // From converts the unit into the receiver. From returns an 37 // error if there is a mismatch in dimension. 38 func (r *Resistance) From(u Uniter) error { 39 if !DimensionsMatch(u, Ohm) { 40 *r = Resistance(math.NaN()) 41 return errors.New("unit: dimension mismatch") 42 } 43 *r = Resistance(u.Unit().Value()) 44 return nil 45 } 46 47 func (r Resistance) Format(fs fmt.State, c rune) { 48 switch c { 49 case 'v': 50 if fs.Flag('#') { 51 fmt.Fprintf(fs, "%T(%v)", r, float64(r)) 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 = " Ω" 59 switch { 60 case pOk && wOk: 61 fmt.Fprintf(fs, "%*.*"+string(c), pos(w-utf8.RuneCount([]byte(unit))), p, float64(r)) 62 case pOk: 63 fmt.Fprintf(fs, "%.*"+string(c), p, float64(r)) 64 case wOk: 65 fmt.Fprintf(fs, "%*"+string(c), pos(w-utf8.RuneCount([]byte(unit))), float64(r)) 66 default: 67 fmt.Fprintf(fs, "%"+string(c), float64(r)) 68 } 69 fmt.Fprint(fs, unit) 70 default: 71 fmt.Fprintf(fs, "%%!%c(%T=%g Ω)", c, r, float64(r)) 72 } 73 }