Mojo Error: constraint failed: the SIMD type must be floating point
note: constraint failed: the SIMD type must be floating point is one of the most confusing messages a beginner meets, because the code that causes it doesn’t mention SIMD at all. It almost always means one thing: a float literal like 2.0, 0.5 or 3.7 ended up somewhere that needs an Int. The fix is to decide which type you really want, and say so. For the other errors in this series, see Mojo Errors Explained.
The error message
Section titled “The error message”def main(): var n = 7 print(n / 2.0)half.mojo:1:5: error: function instantiation faileddef main(): ^half.mojo:3:15: note: call expansion failed print(n / 2.0) ^Mojo/stdlib/std/simd.mojo:983:9: note: constraint failed: the SIMD type must be floating pointmojo: error: failed to run the pass managerThis one reads best from the bottom up:
constraint failed: the SIMD type must be floating pointis the real reason. It comes from Mojo’s own number code insimd.mojo, not from your file.call expansion failedwith the^under2.0shows where in your code it happened: the literal2.0.error: function instantiation failedondef main()only says that buildingmainfailed. It always points at the function, not the bad line.
What it means
Section titled “What it means”In Mojo, both sides of / must have the same type. n is an Int, so Mojo tries to turn the literal 2.0 into an Int too. A literal like 2 can become any number type, but a float literal can only become a floating point type (Float64, Float32 and so on). Mojo’s number types are built on a type called SIMD, and the check that refuses to turn 2.0 into an integer lives there. That’s why the message talks about SIMD.
Mojo’s types and literals cheat sheet says it in one line: float literals can’t become integers, even with explicit typing.
The same message appears for every way a float literal can meet an Int:
| You wrote | Where the ^ points |
|---|---|
n / 2.0, n * 0.5 with var n = 7 |
the float literal |
total += 0.5 with var total = 10 |
0.5 |
var x: Int = 3.7 or Int(3.7) |
3.7 |
set_volume(7.5) where the argument is level: Int |
7.5 |
In Python, 7 / 2.0 quietly gives 3.5. Mojo doesn’t mix integers and floats silently, in either direction.
How to fix it
Section titled “How to fix it”Fix 1: Make the variable a float from the start
Section titled “Fix 1: Make the variable a float from the start”If the value can have decimals, like a price, an average or a total of measurements, create it as a float. Writing 10.0 instead of 10 is enough:
def main(): var total = 10.0 total += 0.5 print(total)Output:
10.5var total = 10.0 makes total a Float64, so adding 0.5 is fine. You can also write var total: Float64 = 10, which turns the literal 10 into a Float64.
Fix 2: Convert the Int before the math
Section titled “Fix 2: Convert the Int before the math”If the variable really is a whole number (a count, an index) and you need a float result, convert it with Float64(...) right where you do the math:
def main(): var n = 7 print(Float64(n) / 2.0) print(Float64(n) * 0.5)Output:
3.53.5Now both sides are Float64, and the literal 2.0 has no problem becoming one.
Fix 3: Use integer math if you wanted whole numbers
Section titled “Fix 3: Use integer math if you wanted whole numbers”Sometimes the .0 was a Python habit and you actually want an Int result. Then drop the decimals from the literal:
def main(): var n = 7 print(n / 2) print(n // 2) print(n % 2)Output:
331With two Int values, / gives an Int and drops the remainder (it rounds toward zero), and // rounds down. They only differ for negative numbers. See Mojo Operators.
Fix 4: Turn a float into an Int from a variable
Section titled “Fix 4: Turn a float into an Int from a variable”Int(3.7) with a literal fails with this same message. But Int(x) works when x is a Float64 variable. It drops the decimals; use round first if you want the nearest whole number:
def main(): var x = 3.7 print(Int(x)) print(Int(round(x)))Output:
34If you only have the literal, just write the whole number: 3 instead of Int(3.7). The Mojo Type Conversion lesson covers Int(...) and round in more detail.
Fix 5: Change the argument type
Section titled “Fix 5: Change the argument type”If a function is called with 7.5 and that should be allowed, the function’s argument type is wrong. Change level: Int to level: Float64:
def set_volume(level: Float64): print("Volume:", level)
def main(): set_volume(7.5)Output:
Volume: 7.5Where you’ll usually see it
Section titled “Where you’ll usually see it”- Percentages and halves.
count * 0.5,price * 1.2orscore / 100.0wherecount,priceorscorewas created from a whole number literal. - Totals that start at
0.var total = 0makes anInt. If you later add0.25to it, start withvar total = 0.0. - Python-style division. In Python,
n / 2.0is the usual way to force float division. In Mojo, writeFloat64(n) / 2.
A cousin of this message: range(n) with a float n fails with constraint failed: a floating-point range requires an explicit step. Loop counters should be Int; see Mojo For Loops.
Related errors
Section titled “Related errors”- cannot implicitly convert ‘X’ value to ‘Y’: an
Intvariable going where aFloat64is needed, the same problem the other way round. - value passed to ‘rhs’ cannot be converted from ‘Int’ to ‘Float64’: what
price * qtygives whenpriceis a float andqtyanInt. - All Mojo error pages: the full list.
Practice
Section titled “Practice”1. Average speed
Section titled “1. Average speed”This program should print the average speed, 82.5. It fails with “the SIMD type must be floating point”. Fix it without changing the numbers.
def main(): var distance = 165 var hours = 2 var speed = distance / hours * 1.0 print(speed)Hint: distance / hours is an Int, and * 1.0 tries to turn 1.0 into an Int. Do the math in Float64 from the start.
Show solution
def main(): var distance = 165 var hours = 2 var speed = Float64(distance) / Float64(hours) print(speed)Output:
82.5Converting both values to Float64 before dividing keeps the .5. Multiplying by 1.0 doesn’t help in Mojo: by then distance / hours has already given the Int 82.
- The message means a float literal like
0.5met anInt, and Mojo can’t make an integer from it. - The
^undercall expansion failedshows the literal; theerror:line only names the function. - Make the variable a float from the start:
var total = 10.0. - Or convert the
Intfirst:Float64(n) / 2.0. - Wanted a whole number? Use
n / 2orn // 2, andInt(x)on aFloat64variable.
Next up: ‘X’ value has no attribute ‘Y’, the error for a method or field that doesn’t exist.
Quick check
Section titled “Quick check”Quick check
Read more
Section titled “Read more”- Types and literals cheat sheet: how literals become types, and why float literals can’t become integers.
- Operators in the Mojo manual:
/,//and%for integers and floats. - Types in the Mojo manual:
Int,Float64and the SIMD types behind them. - Mojo Numbers: our lesson on integers, floats and the math between them.