mirror of
https://github.com/typst/typst
synced 2025-05-14 04:56:26 +08:00
Forbid division by zero
This commit is contained in:
parent
68c6160a14
commit
94cf7005e9
@ -1,10 +1,11 @@
|
|||||||
//! Operations on values.
|
//! Operations on values.
|
||||||
|
|
||||||
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
use super::{Regex, Value};
|
use super::{Regex, Value};
|
||||||
use crate::diag::StrResult;
|
use crate::diag::StrResult;
|
||||||
use crate::geom::{Axes, Axis, GenAlign, Length, Numeric, PartialStroke, Rel, Smart};
|
use crate::geom::{Axes, Axis, GenAlign, Length, Numeric, PartialStroke, Rel, Smart};
|
||||||
use crate::util::format_eco;
|
use crate::util::format_eco;
|
||||||
use std::cmp::Ordering;
|
|
||||||
use Value::*;
|
use Value::*;
|
||||||
|
|
||||||
/// Bail with a type mismatch error.
|
/// Bail with a type mismatch error.
|
||||||
@ -195,6 +196,10 @@ pub fn mul(lhs: Value, rhs: Value) -> StrResult<Value> {
|
|||||||
|
|
||||||
/// Compute the quotient of two values.
|
/// Compute the quotient of two values.
|
||||||
pub fn div(lhs: Value, rhs: Value) -> StrResult<Value> {
|
pub fn div(lhs: Value, rhs: Value) -> StrResult<Value> {
|
||||||
|
if is_zero(&rhs) {
|
||||||
|
Err("cannot divide by zero")?;
|
||||||
|
}
|
||||||
|
|
||||||
Ok(match (lhs, rhs) {
|
Ok(match (lhs, rhs) {
|
||||||
(Int(a), Int(b)) => Float(a as f64 / b as f64),
|
(Int(a), Int(b)) => Float(a as f64 / b as f64),
|
||||||
(Int(a), Float(b)) => Float(a as f64 / b),
|
(Int(a), Float(b)) => Float(a as f64 / b),
|
||||||
@ -229,6 +234,20 @@ pub fn div(lhs: Value, rhs: Value) -> StrResult<Value> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Whether a value is a numeric zero.
|
||||||
|
fn is_zero(v: &Value) -> bool {
|
||||||
|
match *v {
|
||||||
|
Int(v) => v == 0,
|
||||||
|
Float(v) => v == 0.0,
|
||||||
|
Length(v) => v.is_zero(),
|
||||||
|
Angle(v) => v.is_zero(),
|
||||||
|
Ratio(v) => v.is_zero(),
|
||||||
|
Relative(v) => v.is_zero(),
|
||||||
|
Fraction(v) => v.is_zero(),
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Try to divide two lengths.
|
/// Try to divide two lengths.
|
||||||
fn try_div_length(a: Length, b: Length) -> StrResult<f64> {
|
fn try_div_length(a: Length, b: Length) -> StrResult<f64> {
|
||||||
a.try_div(b).ok_or_else(|| "cannot divide these two lengths".into())
|
a.try_div(b).ok_or_else(|| "cannot divide these two lengths".into())
|
||||||
|
@ -33,6 +33,18 @@
|
|||||||
// Error: 2-13 cannot apply '<=' to length and length
|
// Error: 2-13 cannot apply '<=' to length and length
|
||||||
{1em <= 10pt}
|
{1em <= 10pt}
|
||||||
|
|
||||||
|
---
|
||||||
|
// Error: 2-11 cannot divide by zero
|
||||||
|
{1.2 / 0.0}
|
||||||
|
|
||||||
|
---
|
||||||
|
// Error: 2-7 cannot divide by zero
|
||||||
|
{1 / 0}
|
||||||
|
|
||||||
|
---
|
||||||
|
// Error: 2-14 cannot divide by zero
|
||||||
|
{15deg / 0deg}
|
||||||
|
|
||||||
---
|
---
|
||||||
// Special messages for +, -, * and /.
|
// Special messages for +, -, * and /.
|
||||||
// Error: 03-10 cannot add integer and string
|
// Error: 03-10 cannot add integer and string
|
||||||
|
Loading…
x
Reference in New Issue
Block a user