Use bbp crate.

This commit is contained in:
2018-04-24 15:33:21 +02:00
parent c27239c01b
commit 3d7890ff11
5 changed files with 53 additions and 145 deletions

View File

@@ -1,102 +0,0 @@
pub fn get_byte(id: i32) -> u8 {
let pid: f64 = 4.0 * series(1, id) - 2.0 * series(4, id) - series(5, id) - series(6, id);
let y: f64 = (pid - f64::from(pid as i32) + 1.0).abs();
let y = 16.0 * (y - y.floor());
((y as u8) << 4) | (16.0 * (y - y.floor())) as u8
}
fn series(m: i32, id: i32) -> f64 {
const EPS: f64 = 1e-17;
let mut ak: f64;
let mut t: f64;
let mut s: f64 = 0.0;
for k in 0..id {
ak = f64::from(8 * k + m);
t = expm(f64::from(id - k), ak);
s += t / ak;
s -= s.trunc();
}
for k in id..id + 101 {
ak = f64::from(8 * k + m);
t = 16f64.powi(id - k) / ak;
if t < EPS {
break;
}
s += t;
s -= s.trunc();
}
s
}
fn expm(p: f64, ak: f64) -> f64 {
if ak == 1.0 {
return 0.0;
}
const NTP: usize = 25;
lazy_static! {
static ref TP: [f64; NTP] = {
let mut v = [1f64; NTP];
for i in 1..NTP {
v[i] = 2.0 * v[i - 1];
}
v
};
}
let mut i: usize = 0;
while TP[i] <= p {
i += 1;
}
let mut pt = TP[i - 1];
let mut p = p;
let mut r: f64 = 1.0;
for _ in 1..i + 1 {
if p >= pt {
r *= 16.0;
r -= (r / ak).trunc() * ak;
p -= pt;
}
pt *= 0.5;
if pt >= 1.0 {
r *= r;
r -= (r / ak).trunc() * ak;
}
}
r
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_byte() {
assert_eq!(36, get_byte(0));
assert_eq!(67, get_byte(1));
assert_eq!(63, get_byte(2));
assert_eq!(163, get_byte(10));
assert_eq!(41, get_byte(100));
}
}

View File

@@ -1 +0,0 @@

View File

@@ -1,11 +1,8 @@
extern crate bbp;
extern crate byteorder;
extern crate getopts;
#[macro_use]
extern crate lazy_static;
extern crate pbr;
use std::env;
use std::fs;
use std::io::BufReader;
@@ -18,10 +15,6 @@ use getopts::Options;
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use pbr::{ProgressBar, Units};
mod bbp;
struct Pi {
next: i32,
map: BTreeMap<u8, i32>,
@@ -40,7 +33,7 @@ impl Pi {
self.map[&byte]
} else {
for i in self.next..i32::MAX {
if byte == bbp::get_byte(i) {
if byte == bbp::byte_at(i) {
self.map.insert(byte, i);
return i;
@@ -52,7 +45,6 @@ impl Pi {
}
}
fn print_usage(program: &str, opts: &Options) {
let brief = format!("Usage: {} [option] INPUT OUTPUT", program);
print!("{}", opts.usage(&brief));
@@ -156,7 +148,7 @@ fn extract(input: &str, output: &str) {
let mut c = Cursor::new(&buf);
let index = c.read_i32::<LittleEndian>().unwrap();
output_file.write_all(&[bbp::get_byte(index)]).unwrap();
output_file.write_all(&[bbp::byte_at(index)]).unwrap();
pb.add(size as u64);
}