Attempt to multiply with overflow #82
-
Hi, I wanted to port my Chrono code to jiff, but I got pub fn hijri_to_julian_chrono(date: chrono::NaiveDate) -> i32 {
((((11 * date.year() + 3) / 30) as f32).floor()
+ ((354 * date.year()) as f32).floor()
+ ((30 * date.month() as u8) as f32).floor()
- (((date.month() as u8 - 1) / 2) as f32).floor()
+ date.day() as f32
+ 1_948_440.0
- 385.0) as i32
}
pub fn hijri_to_julian_jiff(date: jiff::civil::Date) -> i32 {
((((11 * date.year() + 3) / 30) as f32).floor()
+ ((354 * date.year()) as f32).floor()
+ ((30 * date.month() as u8) as f32).floor()
- (((date.month() as u8 - 1) / 2) as f32).floor()
+ date.day() as f32
+ 1_948_440.0
- 385.0) as i32
}
fn main() {
assert_eq!(
hijri_to_julian_chrono(chrono::NaiveDate::from_ymd_opt(1442, 8, 25).unwrap()),
2459313
);
assert_eq!(
hijri_to_julian_jiff(jiff::civil::date(1442, 8, 25)),
2459313
);
} The original python code is here |
Beta Was this translation helpful? Give feedback.
Answered by
BurntSushi
Aug 7, 2024
Replies: 1 comment 2 replies
-
|
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
azzamsa
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
date.year()
returns ani16
. In this case,1442
. Multiplying that by354
gives510468
, which overflowsi16
. Chrono'sdate.year()
returns ani32
, which doesn't result in an overflow here. So a simple fix is to convert toi32
and then multiply:((354 * i32::from(date.year()))
.