// Code generated by "go generate gonum.org/v1/gonum/unit”; DO NOT EDIT.

// Copyright ©2014 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package unit

import (
	"errors"
	"fmt"
	"math"
	"unicode/utf8"
)

// Power represents a power in Watts.
type Power float64

const (
	Yottawatt Power = 1e24
	Zettawatt Power = 1e21
	Exawatt   Power = 1e18
	Petawatt  Power = 1e15
	Terawatt  Power = 1e12
	Gigawatt  Power = 1e9
	Megawatt  Power = 1e6
	Kilowatt  Power = 1e3
	Hectowatt Power = 1e2
	Decawatt  Power = 1e1
	Watt      Power = 1.0
	Deciwatt  Power = 1e-1
	Centiwatt Power = 1e-2
	Milliwatt Power = 1e-3
	Microwatt Power = 1e-6
	Nanowatt  Power = 1e-9
	Picowatt  Power = 1e-12
	Femtowatt Power = 1e-15
	Attowatt  Power = 1e-18
	Zeptowatt Power = 1e-21
	Yoctowatt Power = 1e-24
)

// Unit converts the Power to a *Unit
func (pw Power) Unit() *Unit {
	return New(float64(pw), Dimensions{
		LengthDim: 2,
		MassDim:   1,
		TimeDim:   -3,
	})
}

// Power allows Power to implement a Powerer interface
func (pw Power) Power() Power {
	return pw
}

// From converts the unit into the receiver. From returns an
// error if there is a mismatch in dimension
func (pw *Power) From(u Uniter) error {
	if !DimensionsMatch(u, Watt) {
		*pw = Power(math.NaN())
		return errors.New("Dimension mismatch")
	}
	*pw = Power(u.Unit().Value())
	return nil
}

func (pw Power) Format(fs fmt.State, c rune) {
	switch c {
	case 'v':
		if fs.Flag('#') {
			fmt.Fprintf(fs, "%T(%v)", pw, float64(pw))
			return
		}
		fallthrough
	case 'e', 'E', 'f', 'F', 'g', 'G':
		p, pOk := fs.Precision()
		w, wOk := fs.Width()
		const unit = " W"
		switch {
		case pOk && wOk:
			fmt.Fprintf(fs, "%*.*"+string(c), pos(w-utf8.RuneCount([]byte(unit))), p, float64(pw))
		case pOk:
			fmt.Fprintf(fs, "%.*"+string(c), p, float64(pw))
		case wOk:
			fmt.Fprintf(fs, "%*"+string(c), pos(w-utf8.RuneCount([]byte(unit))), float64(pw))
		default:
			fmt.Fprintf(fs, "%"+string(c), float64(pw))
		}
		fmt.Fprint(fs, unit)
	default:
		fmt.Fprintf(fs, "%%!%c(%T=%g W)", c, pw, float64(pw))
	}
}
