예제 #1
0
파일: card.go 프로젝트: jbailey/ekcore
func (card *Card) TakeDamage(amount int) (remainingHP int) {
	card.CurrentHitPoints = int(math.Dim(
		float64(card.CurrentHitPoints),
		float64(amount)))

	remainingHP = card.CurrentHitPoints
	return
}
예제 #2
0
파일: card.go 프로젝트: jbailey/ekcore
func (card *Card) LoseAttack(amount int) (remainingAtk int) {
	card.CurrentHitPoints = int(math.Dim(
		float64(card.CurrentAttack),
		float64(amount)))

	remainingAtk = card.CurrentAttack
	return
}
예제 #3
0
func TestMath() {
	fmt.Println(math.Abs(11))
	fmt.Println(math.Abs(-12))
	fmt.Println(math.Abs(-12.34))
	fmt.Println(math.Dim(15, 17))
	fmt.Println(math.Dim(17, 15))

	fmt.Println(math.Max(15, 17))
	fmt.Println(math.Min(15.6, 15.7))

	// 取余
	fmt.Println(math.Mod(10, 4))

	// 返回值1,整数部分,返回值2,小数部分
	fmt.Println(math.Modf(15.6))

	// 幂运算
	fmt.Println(math.Pow(2, 3))

	fmt.Println(math.Sqrt(9))

	fmt.Println("MaxInt8 = ", math.MaxInt8)
	fmt.Println("MinInt8 = ", math.MinInt8)
	fmt.Println("MaxInt16 = ", math.MaxInt16)
	fmt.Println("MinInt16 = ", math.MinInt16)
	fmt.Println("MaxInt32 = ", math.MaxInt32)
	fmt.Println("MinInt32 = ", math.MinInt32)
	fmt.Println("MaxInt64 = ", math.MaxInt64)
	fmt.Println("MinInt64 = ", math.MinInt64)

	fmt.Println("MaxUint8 = ", math.MaxUint8)
	fmt.Println("MaxUint16 = ", math.MaxUint16)
	fmt.Println("MaxUint32 = ", math.MaxUint32)
	// fmt.Println("MaxUint64 = ", math.MaxUint64)

	fmt.Println("MaxInt16 = ", math.MaxInt16)
	fmt.Println("MinInt16 = ", math.MinInt16)

}
예제 #4
0
func ext۰math۰Dim(fr *Frame, args []Value) Value {
	return math.Dim(args[0].(float64), args[1].(float64))
}
예제 #5
0
// Dim returns the maximum of x-y or 0.
//
// Special cases are:
//	Dim(+Inf, +Inf) = NaN
//	Dim(-Inf, -Inf) = NaN
//	Dim(x, NaN) = Dim(NaN, x) = NaN
func Dim(x, y float32) float32 {
	return float32(math.Dim(float64(x), float64(y)))
}
예제 #6
0
파일: simp.go 프로젝트: sguzwf/algorithm
// group like terms in a multiplication
func groupMulTerms(m *Mul) (changed bool) {
	terms := m.CS
	L := len(terms)
	hasConst := false
	var coeff *ConstantF = nil

	for i, x := range terms {
		if x == nil {
			continue
		}

		// setting up current term
		powSum := 1.0

		xT := x.ExprType()
		if xT == CONSTANT && !hasConst {
			hasConst = true
		}

		if xT == CONSTANTF {
			if coeff == nil {
				coeff = x.(*ConstantF)
			} else {
				coeff.F *= x.(*ConstantF).F
				terms[i] = nil
				continue
			}
		}

		var xC Expr
		xTb := NULL
		switch xT {
		case NEG:
			xC = x.(*Neg).C
			xTb = xC.ExprType()
		case POWI:
			p := x.(*PowI)
			xC = p.Base
			xTb = p.Base.ExprType()
			powSum = float64(p.Power)
		case POWF:
			p := x.(*PowF)
			xC = p.Base
			xTb = p.Base.ExprType()
			powSum = p.Power
		case POWE:
			p := x.(*PowE)
			if p.Power.ExprType() == CONSTANTF {
				xC = p.Base
				xTb = p.Base.ExprType()
				powSum = p.Power.(*ConstantF).F
			}
		}

		for j := i + 1; j < L; j++ {
			y := terms[j]
			if y == nil {
				continue
			}

			// setting up following term(s)
			powY := 1.0
			yT := y.ExprType()
			if yT == CONSTANT && !hasConst {
				hasConst = true
			}

			if yT == CONSTANTF {
				if coeff == nil {
					coeff = y.(*ConstantF)
				} else {
					coeff.F *= y.(*ConstantF).F
					terms[j] = nil
					continue
				}
			}

			var yC Expr
			yTb := NULL
			switch yT {
			case NEG:
				yC = y.(*Neg).C
				yTb = yC.ExprType()
			case POWI:
				p := y.(*PowI)
				yC = p.Base
				yTb = p.Base.ExprType()
				powY = float64(p.Power)
			case POWF:
				p := y.(*PowF)
				yC = p.Base
				yTb = p.Base.ExprType()
				powY = p.Power
			case POWE:
				p := y.(*PowE)
				if p.Power.ExprType() == CONSTANTF {
					yC = p.Base
					yTb = p.Base.ExprType()
					powY = p.Power.(*ConstantF).F
				}
			}

			// fmt.Println("Types: ", xT, xTb, yT, yTb)

			// This is the actual comparison Code
			same := false
			if xT == yT {
				if x.AmISame(y) {
					same = true
					powSum += powY
				}
				if xTb == yTb && xTb != NULL {
					if xC.AmISame(yC) {
						same = true
						powSum += powY
					}
				}
			} else if xTb == yT {
				if xC.AmISame(y) {
					same = true
					powSum += powY
				}
			} else if xT == yTb {
				if x.AmISame(yC) {
					same = true
					powSum += powY
				}
			} else if xTb == yTb && xTb != NULL {
				if xC.AmISame(yC) {
					same = true
					powSum += powY
				}
			}

			// Check the results of camparison update the terms
			if same {
				// fmt.Printf("GotHERE\n")
				terms[j] = nil
				changed = true
				if powSum == 0 {
					terms[i] = nil // remove the lhs term
				} else if powSum == 1 {
					// what is lhs?
					// if PowI || Neg || ? :: then extract Base
					if xTb != NULL {
						switch xT {
						case NEG:
							terms[i] = x.(*Neg).C
						case POWI:
							terms[i] = x.(*PowI).Base
						case POWF:
							terms[i] = x.(*PowF).Base
						}
					}
				} else {
					// whole or fractional power?
					flr := math.Floor(powSum)
					dim := math.Dim(powSum, flr)
					if dim == 0 {
						// whole power
						base := x
						if xT != POWI {
							if xTb != NULL {
								switch xT {
								case NEG:
									base = x.(*Neg).C
								case POWF:
									base = x.(*PowF).Base
								}
							}
							base = NewPowI(base, int(powSum))
						} else {
							base.(*PowI).Power = int(powSum)
						}
						terms[i] = base
					} else {
						// fractional power
						base := x
						if xT != POWF {
							if xTb != NULL {
								switch xT {
								case NEG:
									base = x.(*Neg).C
								case POWI:
									base = x.(*PowI).Base
								}
							}
							base = NewPowF(base, powSum)
						} else {
							base.(*PowF).Power = powSum
						}
					}
				}
			}
		}

	}
	return
}
예제 #7
0
파일: book.go 프로젝트: size-of-world/test
func isEqual(f1, f2, p float64) bool {
	return math.Dim(float64(f1), float64(f2)) < p
}
예제 #8
0
// 2.变量类型
func typeDemo() {
	// 变量声明
	var v1 int
	var (
		v2 int
		v3 string
	)
	//var p *int // 指针类型

	// 变量初始化
	var v4 int = 10
	// 等价于:
	var v5 = 10
	// 一般这样就好
	v6 := 10

	// 赋值,多重赋值
	v1 = 10
	v2, v3 = 20, "test"
	// 匿名变量 _
	_, v4 = v5, v6

	fmt.Println(v1, v2, v3, v4)

	// 常量
	const Pi float64 = 3.1415926
	const MaxPlayer = 10

	// 枚举
	const (
		Sunday = iota // iota从0递增
		Mondy
		Tuesday
		// ...
	)

	// 类型
	// 1. 布尔
	var b1 bool
	b1 = true
	b1 = (1 == 2)

	fmt.Println(b1)

	// 2. 整形
	// int8 uint8 int16 uint16 int32 uint32 int64 uint64 int uint uintptr
	var i32 int32
	// 强制转换
	i32 = int32(64)
	// 运算:+, -, *, /, %(求余)
	// 比较:>, <, ==, >=, <=, !=
	// 位运算:x << y, x >> y, x ^ y, x & y, x | y, ^x (取反)

	fmt.Println(i32)

	// 3. 浮点
	// float32, float64
	var f1 float64 = 1.0001
	var f2 float64 = 1.0002
	// 浮点比较
	isEqual := math.Dim(f1, f2) < 0.0001

	fmt.Println(isEqual)

	// 4. 字符串
	var s1 string
	s1 = "abc"
	// 字符串连接
	s1 = s1 + "ddd"
	// 取长度
	n := len(s1)
	// 取字符
	c1 := s1[0]
	// 反引号,不转义,常用于正则表达式
	s1 = `\w+`

	fmt.Println(c1)

	fmt.Println(strings.HasPrefix("prefix", "pre")) // true
	fmt.Println(strings.HasSuffix("suffix", "fix")) // true

	// 字节遍历
	for i := 0; i < n; i++ {
		ch := s1[i]
		fmt.Println(ch)
	}
	// Unicode字符遍历
	for i, ch := range s1 {
		fmt.Println(i, ch)
	}

	// 5. 数组
	var arr1 [32]int
	//var arr2 [3][8]int // 二维数组
	// 初始化
	arr1 = [32]int{0}
	array := [5]int{1, 2, 3, 4, 5}
	// 临时结构体数组
	structArray := []struct {
		name string
		age  int
	}{{"Tim", 18}, {"Jim", 20}}

	// 数组遍历
	for i := 0; i < len(array); i++ {
		fmt.Println(array[i])
	}
	for i, v := range structArray {
		fmt.Println(i, v)
	}
	// 数组是值类型,每次参数传递都是一份拷贝

	// 数组切片Slice
	var mySlice []int = arr1[:2]
	mySlice1 := make([]int, 5)
	mySlice2 := make([]int, 5, 10)

	fmt.Println("len(mySlice2:", len(mySlice2)) // 5
	fmt.Println("cap(mySlice2:", cap(mySlice2)) // 10

	mySlice3 := append(mySlice, 2, 3, 4)
	mySlice4 := append(mySlice, mySlice1...)

	copy(mySlice3, mySlice4)

	// 6. Map
	var m map[int]string
	m[1] = "ddd"
	m1 := make(map[int]string)
	m2 := map[int]string{
		1: "a",
		2: "b",
	}

	delete(m2, 1)

	value, ok := m1[1]
	if ok {
		fmt.Println(value)
	}

	for k, v := range m2 {
		fmt.Println(k, v)
	}

}
예제 #9
0
파일: convert.go 프로젝트: mysll/flynet
func IsEqual(f1, f2 float64) bool {
	return math.Dim(f1, f2) < 0.00001
}