FreeBasic Double Keyword
last modified June 16, 2025
The FreeBasic Double
keyword represents a double-precision
floating-point data type. It provides greater range and precision than
the Single
type, making it suitable for scientific and
financial calculations.
Basic Definition
In FreeBasic, Double
is a 64-bit floating-point data type.
It can store values from approximately ±5.0×10⁻³²⁴ to ±1.7×10³⁰⁸ with
15-16 significant digits of precision.
Double variables are essential for calculations requiring high precision or dealing with very large/small numbers. They follow the IEEE 754 standard for floating-point arithmetic.
Declaring Double Variables
This example shows how to declare and initialize Double variables.
Dim pi As Double = 3.141592653589793 Dim distance As Double = 1.495978707e8 ' km to Sun Dim tinyValue As Double = 1.0e-300 Print "Pi: "; pi Print "Distance to Sun: "; distance; " km" Print "Tiny value: "; tinyValue
Here we declare three Double variables with different magnitudes. The first stores π with high precision. The second shows scientific notation for large numbers. The third demonstrates extremely small values.
Double Arithmetic Operations
Double variables support all standard arithmetic operations with high precision.
Dim x As Double = 123456789.123456789 Dim y As Double = 987654321.987654321 Print "Addition: "; x + y Print "Subtraction: "; y - x Print "Multiplication: "; x * y Print "Division: "; y / x Print "Square root: "; Sqr(x)
This example demonstrates basic arithmetic with Double values. Note how
the operations maintain precision even with large numbers. The Sqr
function returns a Double result.
Double Precision Comparison
Comparing floating-point values requires special consideration due to precision limitations.
Dim a As Double = 0.1 + 0.2 Dim b As Double = 0.3 Print "a = "; a Print "b = "; b Print "a == b: "; (a = b) Print "Approximately equal: "; (Abs(a - b) < 0.0000001)
Due to binary floating-point representation, 0.1 + 0.2 doesn't exactly equal 0.3. This shows why exact equality comparisons often fail with floating-point numbers. We use an epsilon comparison instead.
Double in Mathematical Functions
Many mathematical functions return or accept Double values for precision.
Dim angle As Double = 45.0 ' degrees Dim radians As Double = angle * (3.141592653589793 / 180.0) Print "Sin: "; Sin(radians) Print "Cos: "; Cos(radians) Print "Tan: "; Tan(radians) Print "Log: "; Log(100.0) Print "Exp: "; Exp(1.0)
This demonstrates trigonometric and logarithmic functions using Double values. The functions return Double results, maintaining precision through calculations. Note the degree to radian conversion.
Double Arrays
Arrays of Double values are useful for scientific and engineering data.
Dim temperatures(1 To 5) As Double = { 98.6, 101.2, 97.9, 99.4, 100.1 } Dim sum As Double = 0.0 For i As Integer = 1 To 5 sum += temperatures(i) Next Dim average As Double = sum / 5.0 Print "Average temperature: "; average
This example stores temperature readings in a Double array. We calculate the average while maintaining precision. Double arrays are ideal for numerical data processing.
Double Type Conversion
FreeBasic can convert between Double and other numeric types.
Dim d As Double = 123.456 Dim i As Integer = d Dim s As Single = d Print "Double: "; d Print "To Integer: "; i Print "To Single: "; s Print "Back to Double: "; CDbl(i)
This shows implicit and explicit conversions. Converting to Integer
truncates the decimal. Converting to Single may lose precision. The
CDbl
function converts back to Double.
Double Special Values
Double can represent special floating-point values like infinity and NaN.
Dim inf As Double = 1.0 / 0.0 Dim nan As Double = 0.0 / 0.0 Print "Infinity: "; inf Print "NaN: "; nan Print "Is Infinity: "; IsInfinity(inf) Print "Is NaN: "; IsNaN(nan)
This demonstrates special Double values. Division by zero creates infinity. 0/0 creates NaN (Not a Number). FreeBasic provides functions to detect these special cases.
Best Practices
- Precision: Use Double when Single precision is insufficient.
- Comparisons: Avoid exact equality checks with floating-point.
- Performance: Be aware Double operations may be slower than Single.
- Memory: Consider memory usage with large Double arrays.
- Formatting: Use appropriate formatting when displaying Double values.
This tutorial covered the FreeBasic Double
keyword with practical
examples showing its usage in different scenarios.
Author
List all FreeBasic Tutorials.