Skip to content
This repository was archived by the owner on Dec 9, 2018. It is now read-only.

Commit cdaa3d4

Browse files
author
Jorge Aparicio
committed
feat(Complex): add an I constant, to allow I * 4. + 3. constructors
Note that `3. + 4. * I` doesn't work. See rust-lang/rust#19035
1 parent 1c936a3 commit cdaa3d4

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

src/f32.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//! Single precision
2+
3+
use Complex;
4+
5+
pub const I: Complex<f32> = Complex { re: 0., im: 1. };

src/f64.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//! Double precision
2+
3+
use Complex;
4+
5+
pub const I: Complex<f64> = Complex { re: 0., im: 1. };

src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Complex numbers
22
33
#![deny(missing_docs, warnings)]
4+
#![feature(macro_rules)]
45

56
extern crate onezero;
67

@@ -10,6 +11,9 @@ use std::rand::{Rand, Rng};
1011

1112
use onezero::{One, Zero};
1213

14+
pub mod f32;
15+
pub mod f64;
16+
1317
/// A complex number in Cartesian form.
1418
#[repr(C)]
1519
#[deriving(PartialEq)]
@@ -194,3 +198,22 @@ impl<T> Zero for Complex<T> where T: Zero {
194198
}
195199
}
196200
}
201+
202+
#[cfg(test)]
203+
mod test {
204+
macro_rules! test {
205+
($($ty:ident),+) => {$(
206+
mod $ty {
207+
use Complex;
208+
use $ty::I;
209+
210+
#[test]
211+
fn constructor() {
212+
assert_eq!(Complex::new(3., 4.), I * 4. + 3.)
213+
}
214+
})+
215+
}
216+
}
217+
218+
test!(f32, f64)
219+
}

0 commit comments

Comments
 (0)