Skip to content

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.

def main():
var n = 7
print(n / 2.0)
half.mojo:1:5: error: function instantiation failed
def 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 point
mojo: error: failed to run the pass manager

This one reads best from the bottom up:

  1. constraint failed: the SIMD type must be floating point is the real reason. It comes from Mojo’s own number code in simd.mojo, not from your file.
  2. call expansion failed with the ^ under 2.0 shows where in your code it happened: the literal 2.0.
  3. error: function instantiation failed on def main() only says that building main failed. It always points at the function, not the bad line.

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.

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.5

var 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.

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.5
3.5

Now 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:

3
3
1

With 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:

3
4

If 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.

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.5
  • Percentages and halves. count * 0.5, price * 1.2 or score / 100.0 where count, price or score was created from a whole number literal.
  • Totals that start at 0. var total = 0 makes an Int. If you later add 0.25 to it, start with var total = 0.0.
  • Python-style division. In Python, n / 2.0 is the usual way to force float division. In Mojo, write Float64(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.

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.5

Converting 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.5 met an Int, and Mojo can’t make an integer from it.
  • The ^ under call expansion failed shows the literal; the error: line only names the function.
  • Make the variable a float from the start: var total = 10.0.
  • Or convert the Int first: Float64(n) / 2.0.
  • Wanted a whole number? Use n / 2 or n // 2, and Int(x) on a Float64 variable.

Next up: ‘X’ value has no attribute ‘Y’, the error for a method or field that doesn’t exist.

Quick check

1. With var n = 9, which line gives "the SIMD type must be floating point"?
2. Which line makes total a Float64?
3. Where does the error: line of this message point?