OLD | NEW |
1 /* | 1 /* |
2 This file is part of LilyPond, the GNU music typesetter. | 2 This file is part of LilyPond, the GNU music typesetter. |
3 | 3 |
4 Copyright (C) 1997--2020 Han-Wen Nienhuys <hanwen@xs4all.nl> | 4 Copyright (C) 1997--2020 Han-Wen Nienhuys <hanwen@xs4all.nl> |
5 | 5 |
6 LilyPond is free software: you can redistribute it and/or modify | 6 LilyPond is free software: you can redistribute it and/or modify |
7 it under the terms of the GNU General Public License as published by | 7 it under the terms of the GNU General Public License as published by |
8 the Free Software Foundation, either version 3 of the License, or | 8 the Free Software Foundation, either version 3 of the License, or |
9 (at your option) any later version. | 9 (at your option) any later version. |
10 | 10 |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 } | 88 } |
89 | 89 |
90 Rational::Rational () | 90 Rational::Rational () |
91 { | 91 { |
92 sign_ = 0; | 92 sign_ = 0; |
93 num_ = den_ = 1; | 93 num_ = den_ = 1; |
94 } | 94 } |
95 | 95 |
96 Rational::Rational (I64 n, I64 d) | 96 Rational::Rational (I64 n, I64 d) |
97 { | 97 { |
98 sign_ = ::sign (n) * ::sign (d); | 98 // use sign of n when d=0 |
| 99 sign_ = ::sign (n) * (std::signbit (d) ? -1 : 1); |
99 num_ = ::abs (n); | 100 num_ = ::abs (n); |
100 den_ = ::abs (d); | 101 den_ = ::abs (d); |
101 normalize (); | 102 normalize (); |
102 } | 103 } |
103 | 104 |
104 Rational::Rational (long long n) | 105 Rational::Rational (long long n) |
105 { | 106 { |
106 sign_ = ::sign (n); | 107 sign_ = ::sign (n); |
107 num_ = ::abs (n); | 108 num_ = ::abs (n); |
108 den_ = 1; | 109 den_ = 1; |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 void | 196 void |
196 Rational::normalize () | 197 Rational::normalize () |
197 { | 198 { |
198 if (!sign_) | 199 if (!sign_) |
199 { | 200 { |
200 den_ = 1; | 201 den_ = 1; |
201 num_ = 0; | 202 num_ = 0; |
202 } | 203 } |
203 else if (!den_) | 204 else if (!den_) |
204 { | 205 { |
205 sign_ = 2; | 206 set_infinite (std::signbit (sign_) ? -1 : 1); |
206 num_ = 1; | |
207 } | 207 } |
208 else if (!num_) | 208 else if (!num_) |
209 { | 209 { |
210 sign_ = 0; | 210 sign_ = 0; |
211 den_ = 1; | 211 den_ = 1; |
212 } | 212 } |
213 else | 213 else |
214 { | 214 { |
215 I64 g = gcd (num_, den_); | 215 I64 g = gcd (num_, den_); |
216 | 216 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 return *this; | 271 return *this; |
272 } | 272 } |
273 | 273 |
274 /* | 274 /* |
275 copied from libg++ 2.8.0 | 275 copied from libg++ 2.8.0 |
276 */ | 276 */ |
277 Rational::Rational (double x) | 277 Rational::Rational (double x) |
278 { | 278 { |
279 if (x != 0.0) | 279 if (x != 0.0) |
280 { | 280 { |
| 281 if (std::isinf (x)) |
| 282 { |
| 283 set_infinite (::sign (x)); |
| 284 return; |
| 285 } |
| 286 |
281 sign_ = ::sign (x); | 287 sign_ = ::sign (x); |
282 x *= sign_; | 288 x *= sign_; |
283 | 289 |
284 int expt; | 290 int expt; |
285 double mantissa = frexp (x, &expt); | 291 double mantissa = frexp (x, &expt); |
286 | 292 |
287 const int FACT = 1 << 20; | 293 const int FACT = 1 << 20; |
288 | 294 |
289 /* | 295 /* |
290 Thanks to Afie for this too simple idea. | 296 Thanks to Afie for this too simple idea. |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
377 sign (Rational r) | 383 sign (Rational r) |
378 { | 384 { |
379 return r.sign (); | 385 return r.sign (); |
380 } | 386 } |
381 | 387 |
382 bool | 388 bool |
383 Rational::is_infinity () const | 389 Rational::is_infinity () const |
384 { | 390 { |
385 return sign_ == 2 || sign_ == -2; | 391 return sign_ == 2 || sign_ == -2; |
386 } | 392 } |
OLD | NEW |