ããããè¨äºãéå»ã«æ¸ãããã§ãããã©ãã«ããªãã ããªãã¨ããæ°ããã¾ãã
å°å ¥
ã¨ããã®ãã64 bit ã®æµ®åå°æ°ç¹æ°ã使ãã¨ã㦠90 åããã§ãã¯ããã®ã¯ãããããªãããã§ãã
ï¼IEEE 754 æºæ ã¨ããã®ãä»®å®ããã¨ãã¦ï¼double
ã®ããã表ç¾ã¯ãæ´æ°ã¨ãã¦è¦ã¦ãé åºãããç¨åº¦ä¿ããã¾ãã
ã§ããã°ãæ´æ°ã¨ãã¦è¦ã¦ã«ã¶ãããã¡ãããããªãã¨ãªãã¾ããããããã° 64 åç¨åº¦ã§æ¸ãã®ã§ã
ããã§ãdouble
ã¨ãã¦ã®å¤ã¨ããã表ç¾ã並ã¹ã¦ã¿ã¾ãã
å¤ | ãããè¡¨ç¾ |
---|---|
-NaN |
1111111111111000000000000000000000000000000000000000000000000000 |
-inf |
1111111111110000000000000000000000000000000000000000000000000000 |
-1.7976931348623157e308 |
1111111111101111111111111111111111111111111111111111111111111111 |
-1.0 |
1011111111110000000000000000000000000000000000000000000000000000 |
-2.2250738585072014e-308 |
1000000000010000000000000000000000000000000000000000000000000000 |
-5e-324 |
1000000000000000000000000000000000000000000000000000000000000001 |
-0.0 |
1000000000000000000000000000000000000000000000000000000000000000 |
0.0 |
0000000000000000000000000000000000000000000000000000000000000000 |
5e-324 |
0000000000000000000000000000000000000000000000000000000000000001 |
2.2250738585072014e-308 |
0000000000010000000000000000000000000000000000000000000000000000 |
1.0 |
0011111111110000000000000000000000000000000000000000000000000000 |
1.7976931348623157e308 |
0111111111101111111111111111111111111111111111111111111111111111 |
inf |
0111111111110000000000000000000000000000000000000000000000000000 |
NaN |
0111111111111000000000000000000000000000000000000000000000000000 |
å¤ãè² (-0.0
å«ã) ã§ããã¨ããã«é¢ãã¦ã¯ãããã表ç¾ãæ´æ°ã¨ãã¦èªãã é ã¨éãæ£ (+0.0
å«ã) ã«é¢ãã¦ã¯åãã«ãªã£ã¦ãããã¨ããããã¾ãã
æä¸ä½ãããããããªã«ãããã¨ãèãã次ã®ãããªå¤æãèãã¾ããRust ã§ãã
fn f2u(f: f64) -> u64 { let u = f.to_bits(); if u >> 63 == 1 { !u } else { u | 1 << 63 } }
å¤ã¯æ¬¡ã®ããã«ãªãã¾ãã
f |
f2u(f) ã®ãããè¡¨ç¾ |
---|---|
-NaN |
0000000000000111111111111111111111111111111111111111111111111111 |
-inf |
0000000000001111111111111111111111111111111111111111111111111111 |
-1.7976931348623157e308 |
0000000000010000000000000000000000000000000000000000000000000000 |
-1.0 |
0100000000001111111111111111111111111111111111111111111111111111 |
-2.2250738585072014e-308 |
0111111111101111111111111111111111111111111111111111111111111111 |
-5e-324 |
0111111111111111111111111111111111111111111111111111111111111110 |
-0.0 |
0111111111111111111111111111111111111111111111111111111111111111 |
0.0 |
1000000000000000000000000000000000000000000000000000000000000000 |
5e-324 |
1000000000000000000000000000000000000000000000000000000000000001 |
2.2250738585072014e-308 |
1000000000010000000000000000000000000000000000000000000000000000 |
1.0 |
1011111111110000000000000000000000000000000000000000000000000000 |
1.7976931348623157e308 |
1111111111101111111111111111111111111111111111111111111111111111 |
inf |
1111111111110000000000000000000000000000000000000000000000000000 |
NaN |
1111111111111000000000000000000000000000000000000000000000000000 |
å調å¢å ã«ãªã£ã¦ãããã¨ããããã¾ãããããããNaN ã inf ããå¤å´ã«ããã®ãé½åãããã§ãã
ã¨ããããã§ãããã«é¢ãã¦ã«ã¶ããããã°ããã§ãã
å®è£ ä¾
ABC 026 D ã使ãã¾ãã
f2u
ã®éé¢æ° u2f
ãç¨æãã¦ããã¾ãã
use std::f64::consts::PI; use proconio::input; fn main() { input! { a: f64, b: f64, c: f64, } let f = |x: f64| a * x + b * (c * x * PI).sin(); let res_u = { let mut lb = f2u(0.0); let mut ub = f2u(1.0e9); while ub - lb > 1 { let mid_u = lb + (ub - lb) / 2; let mid = u2f(mid_u); *(if f(mid) < 100.0 { &mut lb } else { &mut ub }) = mid_u; } ub }; let res = u2f(res_u); println!("{}", res); } fn f2u(f: f64) -> u64 { let u = f.to_bits(); if u >> 63 == 1 { !u } else { u ^ 1 << 63 } } fn u2f(u: u64) -> f64 { f64::from_bits(if u >> 63 == 1 { u ^ 1 << 63 } else { !u }) }
æä¸ä½ããããæã£ã¦ãã¦ãã¡ããã¡ããããã¨ãã¯ãç®è¡ã·ããã使ãã° if
ããããªããã§ããã
fn mask(u: u64) -> u64 { ((u as i64 >> 63) as u64 >> 1) | 1 << 63 } fn f2u(f: f64) -> u64 { let u = f.to_bits(); u ^ mask(u) } fn u2f(u: u64) -> f64 { f64::from_bits(u ^ mask(!u)) }
人ã¯ãªã
C++ ã§ã®å®è£
ä¾ããããã¨è¨ãããããªæ°ããã¾ãããC++ ã¯æ¨æºã§ç´æ¥ double
ã®ããã表ç¾ãå¾ãé¢æ°ããªããã§ãããbit_cast
ã¨ããã®ã C++20 ã¾ã§å¾
ã¤å¿
è¦ããããããã§ããããããéã«ãããã¨ããã¨æªå®ç¾©åä½ã«ããªãã¾ããå«ã ããã
ã¨ããã®ã§ãã¡ãã£ã¨é¢åã§ããã以ä¸ãåèã«ãã¤ã¤æ¸ãã¾ãã
æåºã³ã¼ãã®ãªã³ã¯ã ãè¼ãã¦ããã¾ãã
ãããã¡
å®æ°åã«ã¼ãã®æ¹ãç´æçã«æã人ãå¤ããã ãã好ããªæ¹ã使ãã°ããããããªãããªã¨ãæãã¾ãã
ããã¯ããã¨ãã¦ã\( (-\infty, +\infty)\) ã®æ¢ç´¢ããããã¨ããæ´æ°ã«å¤æãã¦ããã°ä¸ãã bit ã決ãã¦ããã°ãããªã£ã¦æ¥½ããã¿ãããªæ°æã¡ãããã¾ããNaN ã«ã¯æ³¨æããå¿ è¦ãããããã
ããã
ããã«ãããã