Basics, 1 of 8
Values and types
Integers, floating-point numbers, Bool, String, and Char, and how As converts a value to another type.
What you’ll build
A program that works out what a car trip costs in fuel. It uses whole numbers for the distance,
decimals for the fuel, a Bool for a yes-or-no answer, and text for the report, and it shows how
FutureBlitz moves a value from one type to another.
Whole numbers and decimals
Every value has a type, and a variable’s type comes from the value it starts with. On its own, a
number without a decimal point is an Int, a 32-bit integer, and a number with one is a Float,
a 32-bit floating-point number.
Import std.core
Local distance = 420 // kilometers, an Int
Local perHundred = 6.5 // liters per 100 km, a Float
Local price = 1.89 // per liter, a Float
Local liters = perHundred * 4.2 // 4.2 hundred kilometers
Print("{distance} km takes {liters} liters")
Print("cost: {liters * price}")420 km takes 27.3 liters cost: 51.596996
A number in a string is written in the shortest form that reads back as the same value, so
27.3 isn’t shown as 27.299999. The cost isn’t exactly 51.597 as a Float, which holds about
seven digits, so it’s shown with every digit it needs; the next step rounds it.
Converting with As
An Int and a Float don’t combine on their own: perHundred * distance is an error, since
turning an Int into a Float can lose digits for large numbers. As converts a value, and
says so in the code. Converting a Float to an Int drops the fraction. A literal is different:
it takes the other side’s type when its value fits, so perHundred * 2 and 7.0 / 2 need no
As. Only two values whose types are already set do.
Import std.core
Local distance = 420
Local perHundred = 6.5
Local price = 1.89
Local liters = perHundred * (distance As Float) / 100
Local cost = liters * price
Print("{distance} km takes {liters} liters, which cost {cost:.2}")
Print("whole liters: {liters As Int}")420 km takes 27.3 liters, which cost 51.60 whole liters: 27
{cost:.2} shows the cost with two digits after the point. A conversion that can’t lose
anything happens without As, like an Int to a Long or a Float to a Double.
More sizes of number
Int and Float are the everyday types. Long is a 64-bit integer, and Double a 64-bit
float, for values too big or too exact for the 32-bit ones. Write the type after a colon to pick
one. A literal takes the type it’s given when its value fits, as 3_000_000_000 does here.
Import std.core
Local seconds: Long = 3_000_000_000 // too big for an Int
Local third: Double = 1.0 / 3.0
Local small: Float = 1.0 / 3.0
Print(seconds)
Print(third)
Print(small)
Print(7 / 2, 7.0 / 2)3000000000 0.3333333333333333 0.33333334 3 3.5
_ separates digits for reading. 7 / 2 divides two Ints, so it’s 3: integer division drops
the remainder. The other integer types are Int8, Int16, UInt8, UInt16, UInt32, and
UInt64, and Byte is another name for UInt8. Int is Int32, Long is Int64, Float is
Float32, and Double is Float64.
Bool, String, and Char
A Bool is True or False, and a comparison gives one. A String is text, and a Char is
one character, written in single quotes. Neither a Bool nor a Char is a number, so each needs
As to become one.
Import std.core
Local distance = 420
Local liters = 6.5 * (distance As Float) / 100
Local tank = 45.0
Local oneTank = liters <= tank
Local unit = 'L'
Local route = "Lyon to Turin"
Print("{route}: {liters}{unit}, on one tank: {oneTank}")
Print(oneTank As Int, unit As Int)Lyon to Turin: 27.3L, on one tank: True 1 76
unit As Int is the character’s code, 76.
When a number doesn’t fit
An integer that grows past its type’s largest value has overflowed. A debug build stops the program there with a panic, which names the operation and the line, instead of going on with a wrong number. A panic ends the program with exit code 101.
Import std.core
Local total: Int = 2_000_000_000
total += total
Print(total)panic: integer overflow in '+' at overflow.fb:4:1
at top-level code (overflow.fb:4)A release build, made with fb build --release, lets overflow wrap around instead, for speed. A
value that needs more room needs a bigger type, like Long.
What you learned
IntandFloatare the default types of whole and decimal literals;LongandDoubleare their 64-bit forms.- Types never change silently in a way that could lose information;
Asconverts and says so. /between integers drops the remainder, andAs Intdrops a Float’s fraction.BoolandChararen’t numbers;As Intgives1or0, or a character’s code.- Integer overflow panics in a debug build, at the line where it happens.
The rules are in the language reference.
Report an issue with this page or with FutureBlitz on GitHub.