Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::convert::{ Into, From };
- enum Integer { I8(i8), I16(i16), I32(i32), U8(u8), U16(u16), U32(u32) }
- enum Float { F32(f32), F64(f64) }
- enum Number { F(Float), I(Integer) }
- impl Into<f32> for Integer {
- fn into(self) -> f32 {
- match self {
- Integer::I8(n) => return n as f32,
- Integer::I16(n) => return n as f32,
- Integer::I32(n) => return n as f32,
- Integer::U8(n) => return n as f32,
- Integer::U16(n) => return n as f32,
- Integer::U32(n) => return n as f32,
- }
- }
- }
- impl Into<f32> for Float {
- fn into(self) -> f32 {
- match self {
- Float::F32(n) => return n,
- Float::F64(n) => return n as f32
- }
- }
- }
- impl Into<f32> for Number {
- fn into(self) -> f32 {
- match self {
- Number::F(n) => return n.into(),
- Number::I(n) => return n.into()
- }
- }
- }
- fn sum<T>(a: T, b: T) -> f32 where T: Into<f32> {
- return a.into() + b.into();
- }
- fn multiply<T>(a: T, b: T) -> f32 where T: Into<f32> {
- return a.into() * b.into();
- }
- fn subtract<T>(a: T, b: T) -> f32 where T: Into<f32> {
- return a.into() - b.into();
- }
- fn divide<T>(a: T, b: T) -> f32 where T: Into<f32> {
- return a.into() / b.into();
- }
- fn pow<T>(base: T, expo: T) -> f32 where T: Into<f32> {
- return base.into().powf(expo.into());
- }
- fn sqrt(value: Number) -> f32 {
- return pow(value, Number::F(Float::F32(0.5)));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement