我有以下关闭内容。
let ss = aa.iter().fold(0., |sum: f32,x| if *x != f32::NAN { sum + e } else { sum + 0. })
我正在尝试对向量中的所有值求和,而忽略f32::NAN
(我在向量中有几个NaN
值)。
但是,我的回报给了我NaN
的价值,因为我的条件if *x != f32::NAN
似乎无效。因为,以下闭包将产生原始向量,而不是忽略NaN
值。
let bb = aa.iter().filter(|x| **x != f32::NAN).map(|x| x)
我的问题是,如何在f32::NAN
条件下匹配if
值?
从更广泛的角度来看,如何在向量中忽略NaN
值?
答案 0 :(得分:3)
您无法通过这种方式检查NaN,因为NaN == NaN
的值为false
。请改用f32::is_nan
。此外,您可以使用Iterator::filter
来过滤出迭代器的元素,并使用Iterator::sum
来对所有值求和。
这将产生以下代码(Playground):
let aa = [3.14f32, std::f32::NAN, 2.71, 27.99];
let ss = aa.iter()
.filter(|n| !n.is_nan())
.sum::<f32>();
println!("{}", ss);
答案 1 :(得分:1)
在Warren Weckesser
的正确指导下,设法使封盖工作。
这是可能需要的人的解决方案。
let ss = aa.iter().fold(0., |sum: f32, x| if x.is_nan() { sum + 0. } else { sum + x });
或者,
let ss = aa.iter().fold(0., |sum: f32, x| if x.is_nan() { sum } else { sum + x });
如果有人关心不必要的+
操作。
性能比较
extern crate rand;
use rand::Rng;
fn main() {
let mut a [f32; 1000] = [0.; 1000];
for i in 0..1000 {
a[i] = rand::thread_rng().gen_range(1,11);
}
}
方法一:
let ss = a.iter()
.filter(|n| !n.is_nan())
.sum::<f32>();
Callgrind
>> cargo profiler callgrind -n 10
Compiling playground in debug mode...
Profiling playground with callgrind...
Total Instructions...3,959,454
177,532 (4.5%) isaac64.rs:rand::prng::isaac64::Isaac64Rng::isaac64
-----------------------------------------------------------------------
121,742 (3.1%) memmove-vec-unaligned-erms.S:memcpy@GLIBC_2.2.5
-----------------------------------------------------------------------
104,224 (2.6%) dl-lookup.c:do_lookup_x
-----------------------------------------------------------------------
102,982 (2.6%) ptr.rs:core::ptr::swap_nonoverlapping_one
-----------------------------------------------------------------------
99,660 (2.5%) intrinsics.rs:core::intrinsics::copy_nonoverlapping
-----------------------------------------------------------------------
76,555 (1.9%) strcmp.S:strcmp
-----------------------------------------------------------------------
72,997 (1.8%) local.rs:_..std..thread..local..LocalKey..T....::try_with
-----------------------------------------------------------------------
72,063 (1.8%) ptr.rs:_..core..ptr..NonNull..T....::as_ref
-----------------------------------------------------------------------
70,028 (1.8%) rc.rs:alloc::rc::RcBoxPtr::strong
-----------------------------------------------------------------------
62,000 (1.6%) ptr.rs:core::ptr::swap_nonoverlapping_one
-----------------------------------------------------------------------
方法II:
let ss = a.iter()
.fold(0., |sum: f32, x| if x.is_nan() { sum } else { sum + x });
Callgrind
>> cargo profiler callgrind -n 10
Compiling playground in debug mode...
Profiling playground with callgrind...
Total Instructions...3,938,312
177,532 (4.5%) isaac64.rs:rand::prng::isaac64::Isaac64Rng::isaac64
-----------------------------------------------------------------------
121,766 (3.1%) memmove-vec-unaligned-erms.S:memcpy@GLIBC_2.2.5
-----------------------------------------------------------------------
104,224 (2.6%) dl-lookup.c:do_lookup_x
-----------------------------------------------------------------------
102,982 (2.6%) ptr.rs:core::ptr::swap_nonoverlapping_one
-----------------------------------------------------------------------
99,660 (2.5%) intrinsics.rs:core::intrinsics::copy_nonoverlapping
-----------------------------------------------------------------------
76,555 (1.9%) strcmp.S:strcmp
-----------------------------------------------------------------------
72,997 (1.9%) local.rs:_..std..thread..local..LocalKey..T....::try_with
-----------------------------------------------------------------------
72,063 (1.8%) ptr.rs:_..core..ptr..NonNull..T....::as_ref
-----------------------------------------------------------------------
70,028 (1.8%) rc.rs:alloc::rc::RcBoxPtr::strong
-----------------------------------------------------------------------
62,000 (1.6%) ptr.rs:core::ptr::swap_nonoverlapping_one
-----------------------------------------------------------------------
在总指令中,方法II 在运行时性能方面表现很好,其中~20,000
指令少于方法I 用于1000
元素数组。预期这种差异将积极地转化为 Approach-II 的增强的运行时性能。调查差异的根源。