github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/pixel/cmc/math.go (about)

     1  package main
     2  
     3  import (
     4  	"math"
     5  )
     6  
     7  const TAU = 2 * math.Pi
     8  
     9  func Clamp1(a float32) float32 {
    10  	if a > 1 {
    11  		return 1
    12  	} else if a < 0 {
    13  		return 0
    14  	}
    15  	return a
    16  }
    17  func Sincos(a float32) (float32, float32) {
    18  	sn, cs := math.Sincos(float64(a))
    19  	return float32(sn), float32(cs)
    20  }
    21  func Sin(a float32) float32  { return float32(math.Sin(float64(a))) }
    22  func Cos(a float32) float32  { return float32(math.Cos(float64(a))) }
    23  func Sqrt(a float32) float32 { return float32(math.Sqrt(float64(a))) }
    24  
    25  func Lerp(a, b, p float32) float32 { return a + (b-a)*p }
    26  
    27  func Log(v float32) float32 { return float32(math.Log(float64(v))) }
    28  func Exp(v float32) float32 { return float32(math.Exp(float64(v))) }
    29  
    30  func Atan2(y, x float32) float32 { return float32(math.Atan2(float64(y), float64(x))) }
    31  
    32  func LogX16(v uint16) float32 { return logx16[v] }
    33  
    34  var logx16 [0xFFFF]float32
    35  
    36  func init() {
    37  	for i := range logx16 {
    38  		logx16[i] = float32(math.Log(float64(i) + 1))
    39  	}
    40  }