| 307 | // type T can be precision decimal (independent of qreal) i.e. float, double, long double |
| 308 | template <typename T> |
| 309 | string printer_toStr(complex<T> num) { |
| 310 | |
| 311 | // precise 0 is rendered as a real integer |
| 312 | if (std::real(num) == 0 && std::imag(num) == 0) |
| 313 | return "0"; |
| 314 | |
| 315 | // real complex-floats neglect imag component |
| 316 | if (std::imag(num) == 0) |
| 317 | return floatToStr(std::real(num)); |
| 318 | |
| 319 | // imaginary complex-floats neglect real component entirely (instead of 0+3i) |
| 320 | string realStr = (std::real(num) == 0)? "" : floatToStr(std::real(num)); |
| 321 | |
| 322 | // scientific-notation real component (when followed by any imaginary component) gets brackets |
| 323 | if (realStr.find('e') != string::npos) |
| 324 | realStr = '(' + realStr + ')'; |
| 325 | |
| 326 | // -1i is abbreviated to -i |
| 327 | if constexpr (std::is_signed_v<T>) |
| 328 | if (std::imag(num) == -1) |
| 329 | return realStr + "-" + IMAGINARY_UNIT; |
| 330 | |
| 331 | // +1i is abbreviated to +i, although the sign is dropped if there's no preceeding real component |
| 332 | if (std::imag(num) == 1) |
| 333 | return realStr + ((std::real(num) == 0)? IMAGINARY_UNIT : ("+" + IMAGINARY_UNIT)); |
| 334 | |
| 335 | // get imag component string but without sign |
| 336 | string imagStr = floatToStr(std::imag(num), true); |
| 337 | |
| 338 | // scientific-notation components always get wrapped in paranthesis |
| 339 | if (imagStr.find('e') != string::npos) |
| 340 | imagStr = '(' + imagStr + ')'; |
| 341 | |
| 342 | // negative imag components always get - sign prefix |
| 343 | if (std::imag(num) < 0) |
| 344 | imagStr = '-' + imagStr; |
| 345 | |
| 346 | // positive imag components only get + sign prefix if they follow a real component |
| 347 | if (std::imag(num) > 0 && std::real(num) != 0) |
| 348 | imagStr = '+' + imagStr; |
| 349 | |
| 350 | // all imag components end in 'i' |
| 351 | imagStr += IMAGINARY_UNIT; |
| 352 | |
| 353 | return realStr + imagStr; |
| 354 | } |
| 355 | |
| 356 | |
| 357 | // explicitly instantiate all publicly passable types |
no test coverage detected