Skip to content

Commit

Permalink
feat+fix
Browse files Browse the repository at this point in the history
* Default trait removed. There was no need to define Default trait for the structure Value.
  This example was working, but not as intended e.g.

  ```Value::new(2.0, Default::deafult())```

  Would actually execute Default trait for the f32 type.

* Provides a method to instantiate a passive variable.
  • Loading branch information
hidal00p committed Jan 31, 2024
1 parent bddcb1c commit f745b29
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/value.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
use std::default::Default;
use std::ops::{Add, Div, Mul, Neg, Sub};

/*
* A wrapper around a numerical value, which
* performs a derivative computation in the tangent mode.
*/

#[derive(Debug, Clone, Copy, Default)]
#[derive(Debug, Clone, Copy)]
pub struct Value {
pub value: f32,
pub der: f32,
}

impl Value {
pub fn passive(value: f32) -> Self {
Value { value, der: 0.0 }
}

pub fn new(value: f32, der: f32) -> Self {
Value { value, der }
}
Expand Down Expand Up @@ -131,7 +134,7 @@ mod test {

#[test]
fn test_default_value() {
let x = Value::new(5.0, Default::default());
let x = Value::passive(5.0);

assert_eq!(x.value, 5.0);
assert_eq!(x.der, 0.0);
Expand Down Expand Up @@ -195,7 +198,7 @@ mod test {
fn f1(x: Value) -> Value {
// f1(x) = 2 * x^3
// f1'(x) = 6 * x^2
let a = Value::new(2.0, Default::default());
let a = Value::passive(2.0);

a * x.pow(3.0)
}
Expand All @@ -212,7 +215,7 @@ mod test {
fn f2(x: Value) -> Value {
// f2(x) = 2 / x^0.5
// f2'(x) = -1 / x^1.5
let a = Value::new(2.0, Default::default());
let a = Value::passive(2.0);

a / x.sqrt()
}
Expand Down

0 comments on commit f745b29

Please sign in to comment.