-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathenum1.rs
More file actions
39 lines (36 loc) · 850 Bytes
/
enum1.rs
File metadata and controls
39 lines (36 loc) · 850 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#[cfg(broken)]
samples::test! {
f32_i32;
enum Foo {
A(f32),
B(i32),
}
#[autodiff(d_bar, Reverse, Duplicated, Active)]
fn bar(x: &f32) -> f32 {
let val: Foo =
if *x > 0.0 {
Foo::A(*x)
} else {
Foo::B(12)
};
std::hint::black_box(&val);
match val {
Foo::A(f) => f * f,
Foo::B(_) => 4.0,
}
}
fn main() {
let x = 1.0;
let x2 = -1.0;
let mut dx = 0.0;
let mut dx2 = 0.0;
let out = bar(&x);
let dout = d_bar(&x, &mut dx, 1.0);
let dout2 = d_bar(&x2, &mut dx2, 1.0);
println!("x: {out}");
println!("dx: {dout}");
println!("dx2: {dout2}");
assert_eq!(2.0, dx);
assert_eq!(0.0, dx2);
}
}