Created
July 16, 2018 12:49
-
-
Save korken89/fd6e2fa5f4ededfdd37cd5e846c7819d to your computer and use it in GitHub Desktop.
Test f32 vs f64 sinf kernel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const S1: f64 = -0.166666666416265235595; /* -0x15555554cbac77.0p-55 */ | |
const S2: f64 = 0.0083333293858894631756; /* 0x111110896efbb2.0p-59 */ | |
const S3: f64 = -0.000198393348360966317347; /* -0x1a00f9e2cae774.0p-65 */ | |
const S4: f64 = 0.0000027183114939898219064; /* 0x16cd878c3b46a7.0p-71 */ | |
pub fn k_sinf(x: f64) -> f32 { | |
let z = x * x; | |
let w = z * z; | |
let r = S3 + z * S4; | |
let s = z * x; | |
((x + s * (S1 + z * S2)) + s * w * r) as f32 | |
} | |
const S1_F32: f32 = -0.166666666416265235595; /* -0x15555554cbac77.0p-55 */ | |
const S2_F32: f32 = 0.0083333293858894631756; /* 0x111110896efbb2.0p-59 */ | |
const S3_F32: f32 = -0.000198393348360966317347; /* -0x1a00f9e2cae774.0p-65 */ | |
const S4_F32: f32 = 0.0000027183114939898219064; /* 0x16cd878c3b46a7.0p-71 */ | |
fn k_sinf_f32(x: f32) -> f32 { | |
let z = x * x; | |
let w = z * z; | |
let r = S3_F32 + z * S4_F32; | |
let s = z * x; | |
((x + s * (S1_F32 + z * S2_F32)) + s * w * r) | |
} | |
use std::f32::consts::PI; | |
fn main() { | |
let mut max_error: f32 = 0.0; | |
for n in 0..100001 { | |
let input = PI / 4.0 / 100000.0 * n as f32; | |
let high_prec = k_sinf(input as f64); | |
let low_prec = k_sinf_f32(input); | |
let error = (high_prec - low_prec).abs(); | |
max_error = max_error.max(error); | |
//println!("Error for input {} = {}", input, error); | |
} | |
println!("Max error = {}", max_error); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment