Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions base/src/functions/mathematical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,4 +682,25 @@ impl Model {
}
CalcResult::Number((x + random() * (y - x)).floor())
}

pub(crate) fn fn_sign(&mut self, args: &[Node], cell: CellReferenceIndex) -> CalcResult {
if args.len() != 1 {
return CalcResult::new_args_number_error(cell);
}
let value = match self.get_number(&args[0], cell) {
Ok(f) => f,
Err(s) => return s,
};
if value.is_nan() {
return CalcResult::Error {
error: Error::NUM,
origin: cell,
message: "Invalid argument for SIGN".to_string(),
};
}
if value == 0.0 {
return CalcResult::Number(0.0);
}
CalcResult::Number(value.signum())
}
}
8 changes: 6 additions & 2 deletions base/src/functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub enum Function {
Round,
Rounddown,
Roundup,
Sign,
Sin,
Sinh,
Sqrt,
Expand Down Expand Up @@ -248,7 +249,7 @@ pub enum Function {
}

impl Function {
pub fn into_iter() -> IntoIter<Function, 194> {
pub fn into_iter() -> IntoIter<Function, 195> {
[
Function::And,
Function::False,
Expand Down Expand Up @@ -287,6 +288,7 @@ impl Function {
Function::Round,
Function::Rounddown,
Function::Roundup,
Function::Sign,
Function::Sum,
Function::Sumif,
Function::Sumifs,
Expand Down Expand Up @@ -542,7 +544,7 @@ impl Function {
"SUM" => Some(Function::Sum),
"SUMIF" => Some(Function::Sumif),
"SUMIFS" => Some(Function::Sumifs),

"SIGN" => Some(Function::Sign),
// Lookup and Reference
"CHOOSE" => Some(Function::Choose),
"COLUMN" => Some(Function::Column),
Expand Down Expand Up @@ -759,6 +761,7 @@ impl fmt::Display for Function {
Function::Sum => write!(f, "SUM"),
Function::Sumif => write!(f, "SUMIF"),
Function::Sumifs => write!(f, "SUMIFS"),
Function::Sign => write!(f, "SIGN"),
Function::Choose => write!(f, "CHOOSE"),
Function::Column => write!(f, "COLUMN"),
Function::Columns => write!(f, "COLUMNS"),
Expand Down Expand Up @@ -991,6 +994,7 @@ impl Model {
Function::Sum => self.fn_sum(args, cell),
Function::Sumif => self.fn_sumif(args, cell),
Function::Sumifs => self.fn_sumifs(args, cell),
Function::Sign => self.fn_sign(args, cell),

// Lookup and Reference
Function::Choose => self.fn_choose(args, cell),
Expand Down
28 changes: 28 additions & 0 deletions base/src/test/test_math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,31 @@ fn test_fn_sqrtpi_arguments() {
assert_eq!(model._get_text("A1"), *"#ERROR!");
assert_eq!(model._get_text("A2"), *"#ERROR!");
}

#[test]
fn test_fn_sign_arguments() {
let mut model = new_empty_model();
model._set("A1", "=SIGN(-42)");
model._set("A2", "=SIGN(13)");
model._set("A3", "=SIGN(0)");
model._set("A4", "=SIGN()");
model._set("A5", "=SIGN('')");
model._set("A6", "=SIGN('Hello')");
model._set("A7", "=SIGN(1, 2)");
model._set("A8", "=SIGN(B8)");
model._set("A9", "=SIGN(4-4)");
model._set("A10", "=SIGN(-0.000001)");

model.evaluate();

assert_eq!(model._get_text("A1"), *"-1");
assert_eq!(model._get_text("A2"), *"1");
assert_eq!(model._get_text("A3"), *"0");
assert_eq!(model._get_text("A4"), *"#ERROR!");
assert_eq!(model._get_text("A5"), *"#ERROR!");
assert_eq!(model._get_text("A6"), *"#ERROR!");
assert_eq!(model._get_text("A7"), *"#ERROR!");
assert_eq!(model._get_text("A8"), *"0");
assert_eq!(model._get_text("A9"), *"0");
assert_eq!(model._get_text("A10"), *"-1");
}
2 changes: 1 addition & 1 deletion docs/src/functions/math-and-trigonometry.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ You can track the progress in this [GitHub issue](https://github.com/ironcalc/Ir
| SECH | <Badge type="info" text="Not implemented yet" /> | – |
| SERIESSUM | <Badge type="info" text="Not implemented yet" /> | – |
| SEQUENCE | <Badge type="info" text="Not implemented yet" /> | – |
| SIGN | <Badge type="info" text="Not implemented yet" /> | – |
| SIGN | <Badge type="tip" text="Available" /> | – |
| SIN | <Badge type="tip" text="Available" /> | – |
| SINH | <Badge type="tip" text="Available" /> | – |
| SQRT | <Badge type="tip" text="Available" /> | – |
Expand Down
3 changes: 1 addition & 2 deletions docs/src/functions/math_and_trigonometry/sign.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ lang: en-US
# SIGN

::: warning
🚧 This function is not yet available in IronCalc.
[Follow development here](https://github.com/ironcalc/IronCalc/labels/Functions)
🚧 This function is implemented but currently lacks detailed documentation. For guidance, you may refer to the equivalent functionality in [Microsoft Excel documentation](https://support.microsoft.com/en-us/office/excel-functions-by-category-5f91f4e9-7b42-46d2-9bd1-63f26a86c0eb).
:::
Loading