ZetCode

FreeBasic Single Keyword

last modified June 16, 2025

The FreeBasic Single keyword represents a single-precision floating-point data type. It is used for storing decimal numbers with approximately 7 digits of precision.

Basic Definition

In FreeBasic, Single is a 32-bit floating-point data type that can represent values from ±1.5 × 10^-45 to ±3.4 × 10^38. It occupies 4 bytes of memory and is less precise than Double.

Single variables are suitable for most general-purpose floating-point calculations where extreme precision isn't required. They offer a good balance between range, precision, and memory usage.

Declaring Single Variables

This example shows how to declare and initialize Single variables.

single_declare.bas
Dim temperature As Single
Dim pi As Single = 3.1415926
Dim gravity As Single = 9.80665

Print "temperature: "; temperature
Print "pi: "; pi
Print "gravity: "; gravity

Here we declare three Single variables. The first is uninitialized and defaults to 0. The others are explicitly set to common physical constants. Note the limited precision in the output compared to the input values.

Single Arithmetic Operations

Single variables support all standard arithmetic operations.

single_arithmetic.bas
Dim x As Single = 12.5
Dim y As Single = 3.2

Print "Addition: "; x + y
Print "Subtraction: "; x - y
Print "Multiplication: "; x * y
Print "Division: "; x / y
Print "Exponentiation: "; x ^ y

This example demonstrates basic arithmetic with Single variables. All operations return Single results. Note that division produces a floating-point result even when dividing whole numbers.

Single Precision Limitations

This example shows the precision limitations of Single variables.

single_precision.bas
Dim preciseValue As Single = 1234567.89
Dim largeValue As Single = 1.23456789e38

Print "Precise value: "; preciseValue
Print "Large value: "; largeValue
Print "Small value: "; 0.000000123456789

The output demonstrates how Single variables lose precision with very large or very small numbers. Only about 7 significant digits are preserved in the first value, while the last value shows rounding in decimal places.

Single Type Conversion

FreeBasic automatically converts between Single and other numeric types.

single_conversion.bas
Dim intValue As Integer = 42
Dim singleValue As Single = intValue

Print "Integer to Single: "; singleValue

Dim newInt As Integer = 3.14159
Dim newSingle As Single = newInt

Print "Single to Integer: "; newInt
Print "Integer back to Single: "; newSingle

This demonstrates implicit type conversion. Integers convert to Single without data loss, but converting Single to Integer truncates the decimal portion. The final conversion shows information loss from the original.

Single in Mathematical Functions

Single variables work with FreeBasic's built-in mathematical functions.

single_math.bas
Dim angle As Single = 45.0
Dim radius As Single = 7.5

Print "Sin: "; Sin(angle * 3.14159 / 180)
Print "Cos: "; Cos(angle * 3.14159 / 180)
Print "Sqrt: "; Sqr(radius * radius)
Print "Log: "; Log(radius)

This shows Single variables used with trigonometric and other math functions. Note the conversion from degrees to radians for the trig functions. All functions return Single precision results.

Single Arrays

Arrays of Single values are useful for scientific and engineering data.

single_array.bas
Dim measurements(1 To 5) As Single
measurements(1) = 12.34
measurements(2) = 56.78
measurements(3) = 90.12
measurements(4) = 34.56
measurements(5) = 78.90

Dim sum As Single = 0
For i As Integer = 1 To 5
    sum += measurements(i)
Next

Print "Average: "; sum / 5

This example creates an array of Single values, fills it with measurements, then calculates the average. Single arrays are memory-efficient for large datasets of floating-point numbers.

Single vs Double

This example compares Single and Double precision.

single_vs_double.bas
Dim singlePi As Single = 3.14159265358979
Dim doublePi As Double = 3.14159265358979

Print "Single precision pi: "; singlePi
Print "Double precision pi: "; doublePi
Print "Difference: "; doublePi - singlePi

The output clearly shows how Single loses precision after 7 digits while Double maintains more digits. The difference calculation quantifies the precision loss when using Single instead of Double.

Best Practices

This tutorial covered the FreeBasic Single keyword with practical examples showing its usage in different scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all FreeBasic Tutorials.