github.com/gopherd/gonum@v0.0.4/unit/dimless.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  )
    14  
    15  // Dimless represents a dimensionless constant.
    16  type Dimless float64
    17  
    18  // Unit converts the Dimless to a *Unit.
    19  func (d Dimless) Unit() *Unit {
    20  	return New(float64(d), Dimensions{})
    21  }
    22  
    23  // Dimless allows Dimless to implement a Dimlesser interface.
    24  func (d Dimless) Dimless() Dimless {
    25  	return d
    26  }
    27  
    28  // From converts the unit into the receiver. From returns an
    29  // error if there is a mismatch in dimension.
    30  func (d *Dimless) From(u Uniter) error {
    31  	if !DimensionsMatch(u, Dimless(0)) {
    32  		*d = Dimless(math.NaN())
    33  		return errors.New("unit: dimension mismatch")
    34  	}
    35  	*d = Dimless(u.Unit().Value())
    36  	return nil
    37  }
    38  
    39  func (d Dimless) Format(fs fmt.State, c rune) {
    40  	switch c {
    41  	case 'v':
    42  		if fs.Flag('#') {
    43  			fmt.Fprintf(fs, "%T(%v)", d, float64(d))
    44  			return
    45  		}
    46  		fallthrough
    47  	case 'e', 'E', 'f', 'F', 'g', 'G':
    48  		p, pOk := fs.Precision()
    49  		w, wOk := fs.Width()
    50  		switch {
    51  		case pOk && wOk:
    52  			fmt.Fprintf(fs, "%*.*"+string(c), w, p, float64(d))
    53  		case pOk:
    54  			fmt.Fprintf(fs, "%.*"+string(c), p, float64(d))
    55  		case wOk:
    56  			fmt.Fprintf(fs, "%*"+string(c), w, float64(d))
    57  		default:
    58  			fmt.Fprintf(fs, "%"+string(c), float64(d))
    59  		}
    60  	default:
    61  		fmt.Fprintf(fs, "%%!%c(%T=%g)", c, d, float64(d))
    62  	}
    63  }