我一直在尝试使用以下函数在m和n之间生成素数:
//the variable sieve is a list of primes between 1 and 32000
//The primes up to 100 are definitely correct
fn sieve_primes(sieve: &Vec<usize>, m: &usize, n: &usize) -> Vec<usize> {
let size: usize = *n - *m + 1;
let mut list: Vec<usize> = Vec::with_capacity(size);
for i in *m..(*n + 1) {
list.push(i);
}
for i in sieve {
for j in ( ((*m as f32) / (*i as f32)).ceil() as usize)..( (((*n as f32) / (*i as f32)).floor() + 1.0) as usize) {
println!("{} ",j);
if j != 1 {list[i * j - *m] = 0;}
}
}
let mut primes: Vec<usize> = Vec::new();
for num in &list{
if *num >= 2 {primes.push(*num);}
}
primes
}
这适用于较小(小于1000000-ish)的m和n值,但是 它在运行时因数十亿/亿的数字而失败。
m = 99999999,n = 100000000的输出为:
33333334
线程&#39;&#39;惊慌失措的指数越界:len是2,但指数是3&#39;
如果你看一下数字,这没有任何意义。首先,它似乎跳过素数列表中的数字2。其次,当i = 3时,for语句应简化为for j in 33333333..333333334
,由于某种原因,它在33333334处开始。
答案 0 :(得分:7)
f32
只能完全代表所有24位整数,相当于大约1600万(实际上是16777216)。除此之外还有差距,最高可达33554432,只能表示偶数。因此,在您的示例中,33333333无法表示为f32
,并且舍入为33333334。
您不需要使用float来舍入整数除法的结果。直接使用整数既快又没有精度问题。对于非负整数,您可以执行以下操作:
fn main() {
let a = 12;
let b = 7;
println!("rounded down: {}", a / b);
println!("rounded: {}", (a + b / 2) / b);
println!("rounded up: {}", (a + b - 1) / b);
}
答案 1 :(得分:2)
您要将整数投射到apply plugin: 'com.android.model.application'
model {
android {
compileSdkVersion 'Google Inc.:Google APIs:23'
buildToolsVersion "23.0.2"
defaultConfig.with {
applicationId "com.ms.sensors"
minSdkVersion.apiLevel 13
targetSdkVersion.apiLevel 23
versionCode 1
versionName "1.0"
}
compileOptions.with {
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
}
buildTypes {
release {
minifyEnabled = false
proguardFiles.add(file('proguard-rules.txt'))
}
}
}
android.ndk {
moduleName = "sensorgraph"
cppFlags.add("-Werror")
ldLibs.addAll(["log", "GLESv2", "android"])
stl = "c++_static"
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:support-v4:23.1.1'
}
,但f32
不够精确。请改用f32
。
f64