github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/unit/unitexample_test.go (about) 1 // Copyright ©2017 The Gonum Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package unit_test 6 7 import ( 8 "fmt" 9 10 "github.com/jingcheng-WU/gonum/unit" 11 ) 12 13 func ExampleNew() { 14 // Create an angular acceleration of 3 rad/s^2 15 accel := unit.New(3.0, unit.Dimensions{unit.AngleDim: 1, unit.TimeDim: -2}) 16 fmt.Println(accel) 17 18 // Output: 3 rad s^-2 19 } 20 21 func ExampleNewDimension() { 22 // Create a "trees" dimension 23 // Typically, this should be used within an init function 24 treeDim := unit.NewDimension("tree") 25 countPerArea := unit.New(0.1, unit.Dimensions{treeDim: 1, unit.LengthDim: -2}) 26 fmt.Println(countPerArea) 27 28 // Output: 0.1 tree m^-2 29 } 30 31 func Example_horsepower() { 32 // One mechanical horsepower ≡ 33,000 ft-lbf/min. 33 foot := unit.Length(0.3048) 34 pound := unit.Mass(0.45359237) 35 36 gravity := unit.Acceleration(9.80665) 37 poundforce := pound.Unit().Mul(gravity) 38 39 hp := ((33000 * foot).Unit().Mul(poundforce)).Div(unit.Minute) 40 fmt.Println("1 hp =", hp) 41 42 watt := unit.Power(1) 43 fmt.Println("W is equivalent to hp?", unit.DimensionsMatch(hp, watt)) 44 45 // Output: 46 // 47 // 1 hp = 745.6998715822701 kg m^2 s^-3 48 // W is equivalent to hp? true 49 }