Skip to content

Added single precision floating point f32#347

Merged
antocuni merged 12 commits into
spylang:mainfrom
Viriathus1:float32
Jan 15, 2026
Merged

Added single precision floating point f32#347
antocuni merged 12 commits into
spylang:mainfrom
Viriathus1:float32

Conversation

@Viriathus1
Copy link
Copy Markdown
Contributor

Added single precision float as type f32 which was mentioned in #305.

Slightly different implementation from the other numerical primitives in that all operations call the C backend directly through WASM exported functions as discussed.

@Viriathus1 Viriathus1 changed the title Added single precision float f32 Added single precision floating point f32 Jan 2, 2026
Copy link
Copy Markdown
Member

@antocuni antocuni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Viriathus1 thank you a lot for this.
It seems mostly complete, but it lacks two things IMHO:

  1. automatic coercion for ieee754_div, see my comment below
  2. conversion rules for f32.

For example, the following two calls should work, but they fail:

def foo(x: f32) -> f32:
    return x

foo(1)
foo(1.2)
TypeError: mismatched types
  | /tmp/x.spy:7
  |     foo(1)
  |         ^ expected `f32`, got `i32`

TypeError: mismatched types
  | /tmp/x.spy:7
  |     foo(1.2)
  |         |_| expected `f32`, got `f64`

similarly, I should be able to convert from f32 to int if I do it explicitly:

def foo(x: f32) -> int:
    return int(x)
TypeError: cannot call objects of type `type`
  | /tmp/x.spy:4
  |     return int(x)
  |            |_| this is `type`

Comment thread spy/libspy/src/operator.c Outdated
Comment thread spy/vm/modules/unsafe/div.py Outdated
@Viriathus1
Copy link
Copy Markdown
Contributor Author

Viriathus1 commented Jan 14, 2026

@antocuni I've added the changes for this one. Sorry I was a bit lazy with the coercion rules mostly because it would be resolved in this issue #333. Let me know if I've missed something

return INT32_MIN;
return (int32_t)x;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good choice here! I think that saturating conversion is what happens in rust and C#, so +1 to do the same in SPY

@antocuni
Copy link
Copy Markdown
Member

@antocuni I've added the changes for this one.

thank you, it looks very good now, merging!

Sorry I was a bit lazy with the coercion rules mostly because it would be resolved in this issue #333. Let me know if I've missed something

no problem :).
I fear it will take a while to fix #333 because it needs some serious language design to understand how to model the "type of a numeric literal before it's coerced to a concrete type".

@antocuni antocuni merged commit 7617b8d into spylang:main Jan 15, 2026
2 checks passed
@Viriathus1 Viriathus1 deleted the float32 branch January 15, 2026 23:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants