package main
import (
"fmt"
"math"
"math/cmplx"
"runtime"
)
const (
Pi = math.Pi
Epsilon = 1e-9
Golden = (1 + math.Sqrt(5)) / 2
)
func sumFloats(nums ...float64) float64 {
sum := 0.0
for _, num := range nums {
sum += num
}
return sum
}
func polarToCartesian(r, theta float64) (x, y float64) {
x = r * math.Cos(theta)
y = r * math.Sin(theta)
return
}
func complexMagnitude(c complex128) float64 {
return cmplx.Abs(c)
}
func deferredDivision(a, b float64) (result float64) {
defer func() {
if b == 0 {
result = math.NaN()
}
}()
result = a / b
return
}
func approximateSqrt(x, guess float64) float64 {
if math.Abs(guess*guess-x) < Epsilon {
return guess
}
return approximateSqrt(x, (guess+x/guess)/2)
}
func precisionDemo() {
a := 0.1
b := 0.2
c := 0.3
fmt.Printf("a + b == c? %v\n", math.Abs((a+b)-c) < Epsilon)
}
func trigonometricMap() map[string]float64 {
return map[string]float64{
"sin(π/4)": math.Sin(Pi / 4),
"cos(π/4)": math.Cos(Pi / 4),
"tan(π/4)": math.Tan(Pi / 4),
}
}
func generateSineWave(freq, amplitude float64, samples int, out chan<- float64) {
for i := 0; i < samples; i++ {
out <- amplitude * math.Sin(2*Pi*freq*float64(i)/float64(samples))
}
close(out)
}
func classifyFloat(x float64) string {
switch {
case math.IsNaN(x):
return "NaN"
case math.IsInf(x, 1):
return "+Inf"
case math.IsInf(x, -1):
return "-Inf"
case x == 0:
return "Zero"
case x > 0:
return "Positive"
default:
return "Negative"
}
}
func findFirstAboveThreshold(threshold float64, values []float64) (float64, bool) {
for _, v := range values {
if v > threshold {
return v, true
}
}
return 0, false
}
func selectFromChannels() {
ch1 := make(chan float64)
ch2 := make(chan float64)
go func() {
ch1 <- math.Pi
close(ch1)
}()
go func() {
ch2 <- math.E
close(ch2)
}()
select {
case v := <-ch1:
fmt.Printf("Received from ch1: %.5f\n", v)
case v := <-ch2:
fmt.Printf("Received from ch2: %.5f\n", v)
}
}
func safeDivision(a, b float64) (result float64) {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered from panic:", r)
result = math.NaN()
}
}()
if b == 0 {
panic("division by zero")
}
return a / b
}
func main() {
fmt.Println("Sum of floats:", sumFloats(1.1, 2.2, 3.3))
x, y := polarToCartesian(1, Pi/4)
fmt.Printf("Polar to Cartesian: (x, y) = (%.2f, %.2f)\n", x, y)
c := complex(3, 4)
fmt.Printf("Magnitude of complex number: %.2f\n", complexMagnitude(c))
fmt.Printf("Deferred division: %.2f\n", deferredDivision(10, 0))
fmt.Printf("Approximate sqrt(2): %.5f\n", approximateSqrt(2, 1))
precisionDemo()
trigMap := trigonometricMap()
for k, v := range trigMap {
fmt.Printf("%s = %.5f\n", k, v)
}
sineWave := make(chan float64)
go generateSineWave(1, 1, 10, sineWave)
fmt.Println("Sine wave samples:")
for sample := range sineWave {
fmt.Printf("%.5f ", sample)
}
fmt.Println()
fmt.Println("Classify float:", classifyFloat(math.NaN()))
values := []float64{0.1, 0.2, 0.3, 0.4, 0.5}
if v, found := findFirstAboveThreshold(0.35, values); found {
fmt.Printf("First value above threshold: %.2f\n", v)
} else {
fmt.Println("No value above threshold found")
}
selectFromChannels()
fmt.Printf("Safe division: %.2f\n", safeDivision(10, 0))
fmt.Printf("Floating-point architecture: %s\n", runtime.GOARCH)
}