ZetCode

FreeBasic Single Keyword

Last modified October 18, 2026

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

Basic Definition

In FreeBasic, Single is a 32-bit floating-point data type that can represent values from ±1.401298 × 10^-45 to ±3.402823 × 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. The basic operators +, -, * and / return Single results, while the exponentiation operator ^ returns a Double, as the longer output shows. 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 (1234567.89 prints as 1234568), while the large value is printed in scientific notation with rounded digits. The small value is printed as 1.23456789e-07, since FreeBASIC uses scientific notation for very small numbers.

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. Small integers such as 42 convert to Single exactly, although very large integers may lose precision because Single has only about 24 bits of mantissa. Converting Single to Integer truncates the decimal portion: 3.14159 becomes 3. The final line shows that the fractional information is lost permanently, since the value stored in newInt is already 3.

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. The math functions return Double results in FreeBASIC, which is why the printed values show more digits than Single precision.

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 about 7 digits (3.141593) while Double maintains more digits. The difference calculation quantifies the precision loss when using Single instead of Double; here it is a small negative value (about -8.74e-08), because the Single value is subtracted from the Double value.

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.