Skip to content

Commit 5c800af

Browse files
committed
fix a bug in under sign on characters
1 parent 6fea90a commit 5c800af

File tree

6 files changed

+65
-1
lines changed

6 files changed

+65
-1
lines changed

src/algorithm/pervade.rs

+48
Original file line numberDiff line numberDiff line change
@@ -1263,6 +1263,54 @@ pub mod mul {
12631263
}
12641264
}
12651265

1266+
pub mod set_sign {
1267+
use super::*;
1268+
1269+
pub fn num_num(a: f64, b: f64) -> f64 {
1270+
mul::num_num(a, b.abs())
1271+
}
1272+
pub fn byte_byte(a: u8, b: u8) -> f64 {
1273+
num_num(a as f64, b as f64)
1274+
}
1275+
pub fn byte_num(a: u8, b: f64) -> f64 {
1276+
num_num(a as f64, b)
1277+
}
1278+
pub fn num_byte(a: f64, b: u8) -> f64 {
1279+
num_num(a, b as f64)
1280+
}
1281+
pub fn num_char(a: f64, b: char) -> char {
1282+
if a > 0.0 {
1283+
scalar_abs::char(b)
1284+
} else if a < 0.0 {
1285+
scalar_neg::char(scalar_abs::char(b))
1286+
} else {
1287+
b
1288+
}
1289+
}
1290+
pub fn char_num(a: char, b: f64) -> char {
1291+
num_char(b, a)
1292+
}
1293+
pub fn byte_char(a: u8, b: char) -> char {
1294+
if a > 0 {
1295+
scalar_abs::char(b)
1296+
} else {
1297+
b
1298+
}
1299+
}
1300+
pub fn char_byte(a: char, b: u8) -> char {
1301+
byte_char(b, a)
1302+
}
1303+
pub fn com_x(a: Complex, b: impl Into<Complex>) -> Complex {
1304+
a.normalize() * b.into()
1305+
}
1306+
pub fn x_com(a: impl Into<Complex>, b: Complex) -> Complex {
1307+
a.into().normalize() * b
1308+
}
1309+
pub fn error<T: Display>(a: T, b: T, env: &Uiua) -> UiuaError {
1310+
env.error(format!("Cannot set sign of {b} to {a}"))
1311+
}
1312+
}
1313+
12661314
pub mod div {
12671315
use super::*;
12681316
pub fn num_num(a: f64, b: f64) -> f64 {

src/compile/invert/under.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ static UNDER_PATTERNS: &[&dyn UnderPattern] = &[
120120
&DupPat,
121121
// Sign ops
122122
&Stash(1, Abs, (Sign, Mul)),
123-
&Stash(1, Sign, (Abs, Mul)),
123+
&Stash(1, Sign, (Flip, SetSign)),
124124
// Mod
125125
&MaybeVal((
126126
Modulus,

src/primitive/defs.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3386,6 +3386,7 @@ impl_primitive!(
33863386
([2], UnBracket),
33873387
([1], UndoRows),
33883388
([1], UndoInventory),
3389+
(2, SetSign),
33893390
// Optimizations
33903391
(1, FirstMinIndex),
33913392
(1, FirstMaxIndex),

src/primitive/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ impl fmt::Display for ImplPrimitive {
269269
UndoRows => write!(f, "{Under}{Rows}"),
270270
UndoInventory => write!(f, "{Under}{Inventory}"),
271271
MaxRowCount(n) => write!(f, "MaxRowCount({n})"),
272+
SetSign => write!(f, "{Under}{Sign}"),
272273
// Optimizations
273274
FirstMinIndex => write!(f, "{First}{Rise}"),
274275
FirstMaxIndex => write!(f, "{First}{Fall}"),
@@ -1538,6 +1539,7 @@ impl ImplPrimitive {
15381539
}
15391540
env.push(max_len.unwrap_or(1));
15401541
}
1542+
ImplPrimitive::SetSign => env.dyadic_oo_env(Value::set_sign)?,
15411543
// Optimizations
15421544
ImplPrimitive::FirstMinIndex => env.monadic_ref_env(Value::first_min_index)?,
15431545
ImplPrimitive::FirstMaxIndex => env.monadic_ref_env(Value::first_max_index)?,

src/value.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1930,6 +1930,13 @@ value_bin_math_impl!(
19301930
(Char, Byte, char_byte),
19311931
[|meta| meta.flags.is_boolean(), Byte, bool_bool],
19321932
);
1933+
value_bin_math_impl!(
1934+
set_sign,
1935+
(Num, Char, num_char),
1936+
(Char, Num, char_num),
1937+
(Byte, Char, byte_char),
1938+
(Char, Byte, char_byte),
1939+
);
19331940
value_bin_math_impl!(div, (Num, Char, num_char), (Byte, Char, byte_char),);
19341941
value_bin_math_impl!(modulus, (Complex, Complex, com_com));
19351942
value_bin_math_impl!(or, [|meta| meta.flags.is_boolean(), Byte, bool_bool]);

tests/under.ua

+6
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,12 @@
231231
⍤⤙≍ ¯5 ⍜±∘ ¯5
232232
⍤⤙≍ 10 ⍜±(+1) 5
233233
⍤⤙≍ ¯10 ⍜±(-1) ¯5
234+
⍤⤙≍ @A ⍜±⋅1 @a
235+
⍤⤙≍ @a ⍜±⋅0 @a
236+
⍤⤙≍ @a ⍜±⋅¯1 @a
237+
⍤⤙≍ @A ⍜±⋅1 @A
238+
⍤⤙≍ @A ⍜±⋅0 @A
239+
⍤⤙≍ @a ⍜±⋅¯1 @A
234240

235241
⍤⤙≍ [6 7 18] ⍜◿(×2) 5 [3 6 14]
236242

0 commit comments

Comments
 (0)