mirror of
https://github.com/gonum/gonum.git
synced 2025-10-16 20:20:41 +08:00
unit: add Torque type
This commit is contained in:
@@ -40,6 +40,7 @@ var definedTypes = []struct {
|
||||
{unit: unit.Resistance(1), name: "unit.Resistance"},
|
||||
{unit: unit.Temperature(1), name: "unit.Temperature"},
|
||||
{unit: unit.Time(1), name: "unit.Time"},
|
||||
{unit: unit.Torque(1), name: "unit.Torque"},
|
||||
{unit: unit.Velocity(1), name: "unit.Velocity"},
|
||||
{unit: unit.Voltage(1), name: "unit.Voltage"},
|
||||
{unit: unit.Volume(1), name: "unit.Volume"},
|
||||
|
11
unit/doc.go
11
unit/doc.go
@@ -104,17 +104,12 @@
|
||||
// Different physical ideas are sometimes expressed with the same dimensions
|
||||
// and unit is incapable of catching these mismatches. For example, energy and
|
||||
// torque are both expressed as force times distance (Newton-meters in SI),
|
||||
// but it is wrong to say that a torque of 10 N-m is the same as 10 J, even
|
||||
// but it is wrong to say that a torque of 10 N·m is the same as 10 J, even
|
||||
// though the dimensions agree. Despite this, using the defined types to
|
||||
// represent units can help to catch errors at compile-time. For example,
|
||||
// using unit.Torque allows you to define a statically typed function like so
|
||||
//
|
||||
// type Torque float64
|
||||
//
|
||||
// func (t Torque) Unit() *Unit {...
|
||||
//
|
||||
// allows you to define a statically typed function like so
|
||||
//
|
||||
// func LeverLength(apply unit.Force, want Torque) unit.Length {
|
||||
// func LeverLength(apply unit.Force, want unit.Torque) unit.Length {
|
||||
// return unit.Length(float64(want)/float64(apply))
|
||||
// }
|
||||
//
|
||||
|
@@ -480,6 +480,20 @@ var Units = []Unit{
|
||||
},
|
||||
ErForm: "Pressurer",
|
||||
},
|
||||
{
|
||||
Name: "Torque",
|
||||
Receiver: "t",
|
||||
PrintString: "N m",
|
||||
Suffix: "newtonmeter",
|
||||
Singular: "Newtonmeter",
|
||||
TypeComment: "Torque represents a torque in Newton meters",
|
||||
Dimensions: []Dimension{
|
||||
{Name: LengthName, Power: 2},
|
||||
{Name: MassName, Power: 1},
|
||||
{Name: TimeName, Power: -2},
|
||||
},
|
||||
ErForm: "Torquer",
|
||||
},
|
||||
{
|
||||
Name: "Velocity",
|
||||
Receiver: "v",
|
||||
|
94
unit/torque.go
Normal file
94
unit/torque.go
Normal file
@@ -0,0 +1,94 @@
|
||||
// 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"
|
||||
)
|
||||
|
||||
// Torque represents a torque in Newton meters.
|
||||
type Torque float64
|
||||
|
||||
const (
|
||||
Yottanewtonmeter Torque = 1e24
|
||||
Zettanewtonmeter Torque = 1e21
|
||||
Exanewtonmeter Torque = 1e18
|
||||
Petanewtonmeter Torque = 1e15
|
||||
Teranewtonmeter Torque = 1e12
|
||||
Giganewtonmeter Torque = 1e9
|
||||
Meganewtonmeter Torque = 1e6
|
||||
Kilonewtonmeter Torque = 1e3
|
||||
Hectonewtonmeter Torque = 1e2
|
||||
Decanewtonmeter Torque = 1e1
|
||||
Newtonmeter Torque = 1.0
|
||||
Decinewtonmeter Torque = 1e-1
|
||||
Centinewtonmeter Torque = 1e-2
|
||||
Millinewtonmeter Torque = 1e-3
|
||||
Micronewtonmeter Torque = 1e-6
|
||||
Nanonewtonmeter Torque = 1e-9
|
||||
Piconewtonmeter Torque = 1e-12
|
||||
Femtonewtonmeter Torque = 1e-15
|
||||
Attonewtonmeter Torque = 1e-18
|
||||
Zeptonewtonmeter Torque = 1e-21
|
||||
Yoctonewtonmeter Torque = 1e-24
|
||||
)
|
||||
|
||||
// Unit converts the Torque to a *Unit
|
||||
func (t Torque) Unit() *Unit {
|
||||
return New(float64(t), Dimensions{
|
||||
LengthDim: 2,
|
||||
MassDim: 1,
|
||||
TimeDim: -2,
|
||||
})
|
||||
}
|
||||
|
||||
// Torque allows Torque to implement a Torquer interface
|
||||
func (t Torque) Torque() Torque {
|
||||
return t
|
||||
}
|
||||
|
||||
// From converts the unit into the receiver. From returns an
|
||||
// error if there is a mismatch in dimension
|
||||
func (t *Torque) From(u Uniter) error {
|
||||
if !DimensionsMatch(u, Newtonmeter) {
|
||||
*t = Torque(math.NaN())
|
||||
return errors.New("Dimension mismatch")
|
||||
}
|
||||
*t = Torque(u.Unit().Value())
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t Torque) Format(fs fmt.State, c rune) {
|
||||
switch c {
|
||||
case 'v':
|
||||
if fs.Flag('#') {
|
||||
fmt.Fprintf(fs, "%T(%v)", t, float64(t))
|
||||
return
|
||||
}
|
||||
fallthrough
|
||||
case 'e', 'E', 'f', 'F', 'g', 'G':
|
||||
p, pOk := fs.Precision()
|
||||
w, wOk := fs.Width()
|
||||
const unit = " N m"
|
||||
switch {
|
||||
case pOk && wOk:
|
||||
fmt.Fprintf(fs, "%*.*"+string(c), pos(w-utf8.RuneCount([]byte(unit))), p, float64(t))
|
||||
case pOk:
|
||||
fmt.Fprintf(fs, "%.*"+string(c), p, float64(t))
|
||||
case wOk:
|
||||
fmt.Fprintf(fs, "%*"+string(c), pos(w-utf8.RuneCount([]byte(unit))), float64(t))
|
||||
default:
|
||||
fmt.Fprintf(fs, "%"+string(c), float64(t))
|
||||
}
|
||||
fmt.Fprint(fs, unit)
|
||||
default:
|
||||
fmt.Fprintf(fs, "%%!%c(%T=%g N m)", c, t, float64(t))
|
||||
}
|
||||
}
|
33
unit/torque_test.go
Normal file
33
unit/torque_test.go
Normal file
@@ -0,0 +1,33 @@
|
||||
// Code generated by "go generate gonum.org/v1/gonum/unit; DO NOT EDIT.
|
||||
|
||||
// Copyright ©2019 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 (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTorqueFormat(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
value Torque
|
||||
format string
|
||||
want string
|
||||
}{
|
||||
{1.23456789, "%v", "1.23456789 N m"},
|
||||
{1.23456789, "%.1v", "1 N m"},
|
||||
{1.23456789, "%20.1v", " 1 N m"},
|
||||
{1.23456789, "%20v", " 1.23456789 N m"},
|
||||
{1.23456789, "%1v", "1.23456789 N m"},
|
||||
{1.23456789, "%#v", "unit.Torque(1.23456789)"},
|
||||
{1.23456789, "%s", "%!s(unit.Torque=1.23456789 N m)"},
|
||||
} {
|
||||
got := fmt.Sprintf(test.format, test.value)
|
||||
if got != test.want {
|
||||
t.Errorf("Format %q %v: got: %q want: %q", test.format, float64(test.value), got, test.want)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user