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