mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-30 07:14:46 +00:00
implement negation
This commit is contained in:
parent
182fd5201d
commit
e5e38f03bc
1 changed files with 16 additions and 2 deletions
|
@ -80,7 +80,17 @@ pub fn float_expr_from_result(
|
|||
pub fn finish_parsing_int(raw: &str, is_negative: bool) -> Result<i64, (&str, IntErrorKind)> {
|
||||
// Ignore underscores.
|
||||
let radix = 10;
|
||||
from_str_radix::<i64>(raw.replace("_", "").as_str(), radix).map_err(|e| (raw, e.kind))
|
||||
let number =
|
||||
from_str_radix::<i64>(raw.replace("_", "").as_str(), radix).map_err(|e| (raw, e.kind))?;
|
||||
|
||||
if is_negative {
|
||||
match number.checked_neg() {
|
||||
None => Err((raw, IntErrorKind::Overflow)),
|
||||
Some(value) => Ok(value),
|
||||
}
|
||||
} else {
|
||||
Ok(number)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
|
@ -108,7 +118,11 @@ pub fn finish_parsing_base(
|
|||
#[inline(always)]
|
||||
pub fn finish_parsing_float(raw: &str, is_negative: bool) -> Result<f64, (&str, FloatErrorKind)> {
|
||||
// Ignore underscores.
|
||||
match raw.replace("_", "").parse::<f64>() {
|
||||
match raw
|
||||
.replace("_", "")
|
||||
.parse::<f64>()
|
||||
.map(|x| if is_negative { -x } else { x })
|
||||
{
|
||||
Ok(float) if float.is_finite() => Ok(float),
|
||||
Ok(float) => {
|
||||
if float.is_sign_positive() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue