| | |
| | | from . import Exceptions |
| | | |
| | | from math import floor, log10 |
| | | |
| | | class FormatterFactory: |
| | | @staticmethod |
| | |
| | | |
| | | else: |
| | | return None |
| | | |
| | | @staticmethod |
| | | def fexp(f): |
| | | return int(floor(log10(abs(f)))) if f != 0 else 0 |
| | | |
| | | @staticmethod |
| | | def fman(f): |
| | | return f/10**FormatterFactory.fexp(f) |
| | | |
| | | |
| | | class SciFloatFormatter(FormatterFactory): |
| | |
| | | except TypeError: |
| | | raise ValueError |
| | | |
| | | return ("${:." + str(self.precision) + "f}$").format(num) |
| | | exp=self.fexp(num) |
| | | man=self.fman(num) |
| | | |
| | | return ("${:." + str(self.precision-1) + "f} \cdot 10^{{{}}}$").format(man, int(exp)) |
| | | |
| | | |
| | | class EngFloatFormatter(FormatterFactory): |
| | |
| | | except (ValueError, TypeError): |
| | | raise ValueError |
| | | # TODO: Change formatting string |
| | | |
| | | return ("${:." + str(self.precision) + "f}$").format(num) |
| | |
| | | -1.123e6, |
| | | 9.8123e255, |
| | | 0, |
| | | "-NaN", |
| | | "+NaN", |
| | | ] |
| | | sci2_solutions = [ |
| | | "$1.24$", |
| | | "$1.20$", |
| | | "$3.00$", |
| | | "$3.50$", |
| | | "$0.05$", |
| | | "$-1.23$", |
| | | "$-3.00$", |
| | | "$1.23 \cdot 10^{-6}$", |
| | | "$-1.23 \cdot 10^{6}$", |
| | | "$9.91 \cdot 10^{255}$", |
| | | "$0.00$", |
| | | "ni rezultata", |
| | | "ni rezultata", |
| | | "$1.2 \cdot 10^{0}$", |
| | | "$1.2 \cdot 10^{0}$", |
| | | "$3.0 \cdot 10^{0}$", |
| | | "$3.5 \cdot 10^{0}$", |
| | | "$5.0 \cdot 10^{-2}$", |
| | | "$-1.2 \cdot 10^{0}$", |
| | | "$-3.0 \cdot 10^{0}$", |
| | | "$1.1 \cdot 10^{-6}$", |
| | | "$-1.1 \cdot 10^{6}$", |
| | | "$9.8 \cdot 10^{255}$", |
| | | "$0.0 \cdot 10^{0}$", |
| | | ] |
| | | |
| | | sci3_solutions = [ |
| | | "$1.24 \cdot 10^{0}$", |
| | | "$1.20 \cdot 10^{0}$", |
| | | "$3.00 \cdot 10^{0}$", |
| | | "$3.50 \cdot 10^{0}$", |
| | | "$5.00 \cdot 10^{-2}$", |
| | | "$-1.23 \cdot 10^{0}$", |
| | | "$-3.00 \cdot 10^{0}$", |
| | | "$1.12 \cdot 10^{-6}$", |
| | | "$-1.12 \cdot 10^{6}$", |
| | | "$9.81 \cdot 10^{255}$", |
| | | "$0.00 \cdot 10^{0}$", |
| | | ] |
| | | sci2 = Formatter.FormatterFactory().get_formatter("sci 2") |
| | | for case, result in zip(testcases, sci2_solutions): |
| | | self.assertEqual(sci2.toFormat(case), result) |
| | | for case, result in zip(testcases,sci2_solutions): |
| | | self.assertEqual(result, sci2.toFormat(case)) |
| | | |
| | | sci3 = Formatter.FormatterFactory().get_formatter("sci 3") |
| | | for case, result in zip(testcases,sci3_solutions): |
| | | self.assertEqual(result, sci3.toFormat(case)) |