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