/src/lib/numeric/internal/fraction_general_number.e
Specman e | 161 lines | 117 code | 18 blank | 26 comment | 7 complexity | d0a818207a6c390bf56b39b9ab11a3bb MD5 | raw file
1-- This file is part of a Liberty Eiffel library. 2-- See the full copyright at the end. 3-- 4deferred class FRACTION_GENERAL_NUMBER 5 -- 6 -- To implement NUMBER (do not use this class, see NUMBER). 7 -- 8 9inherit 10 NUMBER 11 12feature {ANY} 13 is_zero: BOOLEAN False 14 15 is_one: BOOLEAN False 16 17 is_positive: BOOLEAN 18 do 19 Result := not is_negative 20 end 21 22 is_negative: BOOLEAN 23 do 24 Result := numerator.is_negative 25 end 26 27 factorial: NUMBER 28 do 29 check 30 False 31 end 32 end 33 34 infix "@=" (other: INTEGER_64): BOOLEAN 35 do 36 end 37 38 infix "//" (other: NUMBER): NUMBER 39 do 40 check 41 False 42 end 43 end 44 45 infix "@//" (other: INTEGER_64): NUMBER 46 do 47 check 48 False 49 end 50 end 51 52 infix "\\" (other: NUMBER): NUMBER 53 do 54 check 55 False 56 end 57 end 58 59 infix "@\\" (other: INTEGER_64): NUMBER 60 do 61 check 62 False 63 end 64 end 65 66feature {ANY} -- Misc: 67 hash_code: INTEGER 68 do 69 Result := numerator.hash_code #+ denominator.hash_code 70 if Result < 0 then 71 Result := ~Result 72 end 73 end 74 75 gcd (other: NUMBER): INTEGER_GENERAL_NUMBER 76 do 77 check 78 False 79 end 80 end 81 82feature {} 83 decimal_in (buffer: STRING; num, denom: NUMBER; negative: BOOLEAN; digits: INTEGER; all_digits: BOOLEAN) 84 local 85 n, div, remainder: NUMBER; counter: INTEGER 86 do 87 if is_negative then 88 buffer.extend('-') 89 n := -num 90 else 91 n := num 92 end 93 div := n // denom 94 div.append_in(buffer) 95 remainder := n \\ denom 96 if digits > 0 then 97 if all_digits or else not (remainder @= 0) then 98 buffer.extend('.') 99 from 100 counter := 1 101 until 102 counter > digits or else remainder @= 0 103 loop 104 n := remainder @* 10 105 ;(n // denom).append_in(buffer) 106 remainder := n \\ denom 107 counter := counter + 1 108 end 109 if all_digits then 110 from 111 until 112 counter > digits 113 loop 114 buffer.extend('0') 115 counter := counter + 1 116 end 117 end 118 end 119 end 120 end 121 122feature {NUMBER} -- Implementation: 123 gcd_with_integer_64_number (other: INTEGER_64_NUMBER): INTEGER_GENERAL_NUMBER 124 do 125 check 126 False 127 end 128 end 129 130 gcd_with_big_integer_number (other: BIG_INTEGER_NUMBER): INTEGER_GENERAL_NUMBER 131 do 132 check 133 False 134 end 135 end 136 137invariant 138 denominator @>= 2 139 not numerator.is_zero 140 141end -- class FRACTION_GENERAL_NUMBER 142-- 143-- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file. 144-- 145-- Permission is hereby granted, free of charge, to any person obtaining a copy 146-- of this software and associated documentation files (the "Software"), to deal 147-- in the Software without restriction, including without limitation the rights 148-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 149-- copies of the Software, and to permit persons to whom the Software is 150-- furnished to do so, subject to the following conditions: 151-- 152-- The above copyright notice and this permission notice shall be included in 153-- all copies or substantial portions of the Software. 154-- 155-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 156-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 157-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 158-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 159-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 160-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 161-- THE SOFTWARE.