rustgd / cgmath

A linear algebra and mathematics library for computer graphics.
https://docs.rs/cgmath
Apache License 2.0
1.12k stars 155 forks source link

Remove NumCast from BaseNum and relax float bounds #537

Closed spearman closed 3 years ago

spearman commented 3 years ago

The first commit removes the NumCast from the BaseNum trait. This is enough to allow using non-machine scalars (e.g. fixed precision numbers) for vector/point operations:

#![feature(array_map)]

use cgmath::InnerSpace;
use fixed::types::I16F16 as Ftype;

macro_rules! show {
    ($e:expr) => {
        println!("{}: {:?}", stringify!($e), $e);
    };
}

fn main() {
    let p1: cgmath::Point2<Ftype> = [0, 1].map(Ftype::from_num).into();
    show!(p1);
    let p2: cgmath::Point2<Ftype> = [1, 0].map(Ftype::from_num).into();
    show!(p2);
    show!(p2 - p1);
    let v: cgmath::Vector2<Ftype> = [1, 1].map(Ftype::from_num).into();
    show!(v);
    show!(p1 + v);
    show!(v.magnitude2());
    show!(v.dot(v));
}

The second commit replaces uses of BaseFloat and Float with BaseNum and Num where possible. This might help with making finer grained trait bounds down the road (https://github.com/rustgd/cgmath/issues/496).

kvark commented 3 years ago

I assume this is a non-breaking change?

spearman commented 3 years ago

I assume this is a non-breaking change?

I think so, maybe someone can confirm.