How to generate a range of floating-point numbers in Go

3 Answers

0 votes
package main

import "fmt"

func main() {

	/*
		==================================================
		Title: Generate a Range of Floating-Point Numbers
		==================================================

		Best Practice:
		- Use an integer loop counter.
		- Compute each floating-point value from the index.
		- Avoid repeatedly adding the step value because
		  floating-point rounding errors can accumulate.
	*/

	// Range configuration.
	const start float64 = 0.0
	const step float64 = 0.1
	const count int = 11 // Generates values from 0.0 to 1.0 inclusive.

	/*
		Use an integer loop variable.

		Each value is calculated as:

			start + index * step

		Instead of:

			value += step

		This helps avoid accumulated rounding errors.
	*/
	for i := 0; i < count; i++ {

		// Compute the current value independently.
		value := start + float64(i)*step

		// Format output to one decimal place.
		fmt.Printf("%.1f\n", value)
	}
}


/*
run:

0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0

*/

 



answered Oct 7 by avibootz
0 votes
package main

import (
	"fmt"
)

/*
	==================================================
	Title: Reusable Floating-Point Range Function
	==================================================

	This function:

	- Validates inputs
	- Returns a slice of float64 values
	- Uses integer-controlled iteration
	- Avoids cumulative floating-point errors
*/
func GenerateRange(
	start float64,
	end float64,
	step float64,
) ([]float64, error) {

	// Validate input arguments.
	if step <= 0 {
		return nil, fmt.Errorf("step must be positive")
	}

	if end < start {
		return nil, fmt.Errorf(
			"end must be greater than or equal to start",
		)
	}

	/*
		Determine how many values are needed.

		Example:

		start = 0.0
		end   = 1.0
		step  = 0.1

		count = 11
	*/
	count := int((end-start)/step) + 1

	// Preallocate capacity to avoid reallocations.
	values := make([]float64, 0, count)

	for i := 0; i < count; i++ {

		// Calculate the value from the index.
		value := start + float64(i)*step

		values = append(values, value)
	}

	return values, nil
}

func main() {

	values, err := GenerateRange(0.0, 1.0, 0.1)
	if err != nil {
		fmt.Println(err)
		return
	}

	for _, value := range values {
		fmt.Printf("%.1f\n", value)
	}
}


/*
run:

0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0

*/

 



answered Oct 7 by avibootz
0 votes
package main

import "fmt"

/*
	==================================================
	Title: Generator-Style Floating-Point Range
	==================================================

	Values are produced lazily through a channel.
	This is useful when the range is large and you
	do not want to allocate a large slice.
*/
func GenerateRange(
	start float64,
	end float64,
	step float64,
) <-chan float64 {

	ch := make(chan float64)

	go func() {
		defer close(ch)

		count := int((end-start)/step) + 1

		for i := 0; i < count; i++ {
			ch <- start + float64(i)*step
		}
	}()

	return ch
}

func main() {

	for value := range GenerateRange(
		0.0,
		1.0,
		0.1,
	) {
		fmt.Printf("%.1f\n", value)
	}
}


/*
run:

0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0

*/

 



answered Oct 7 by avibootz
...