Skip to content

An unannotated float const takes f64 instead of its use's type; using it as f32 is rejected as narrowing #2096

Closed

Description

@ehartford

Repro (with v0.15.3.0, macOS arm64)

const A = 1.0 / 120.0
const B = 12.0

fn takes32(x: f32) -> f32: x

fn main:
    let y = takes32(A)
    let z: f32 = B
    print(f"{y} {z}")
error: implicit float narrowing from `f64` to `f32`; use an explicit `as` cast (§4.2.6)
 --> infer.w:7:21
7 |     let y = takes32(A)
error: implicit float narrowing from `f64` to `f32`; use an explicit `as` cast (§4.2.6)
 --> infer.w:8:18
8 |     let z: f32 = B

Expected

Both lines compile, with A and B taking f32 from their use. An unsuffixed float literal is f64 only when nothing says otherwise. Here the f32 parameter and the f32 binding say otherwise. The constants have no type of their own to narrow from: the language's inference should give them f32, as it would give the literals themselves (takes32(1.0 / 120.0) and let z: f32 = 12.0 are fine).

Impact

A raylib program, where everything is f32 (Vector2, DrawCircleV), has to annotate every float constant (const SHIP_SPEED: f32 = 320.0) or cast at each use. Found writing a tutorial game for the WIPE repo (withlang-dev/wipe).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions