Changeset 1115
 Timestamp:
 Dec 22, 2011, 9:09:46 PM (11 years ago)
 Location:
 trunk
 Files:

 3 edited
Legend:
 Unmodified
 Added
 Removed

trunk/src/real.cpp
r1029 r1115 120 120 121 121 return u.d; 122 } 123 124 /* 125 * Create a real number from an ASCII representation 126 */ 127 real::real(char const *str) 128 { 129 real ret = 0; 130 int exponent = 0; 131 bool comma = false, nonzero = false, negative = false, finished = false; 132 133 for (char const *p = str; *p && !finished; p++) 134 { 135 switch (*p) 136 { 137 case '': 138 case '+': 139 if (p != str) 140 break; 141 negative = (*p == ''); 142 break; 143 case '.': 144 if (comma) 145 finished = true; 146 comma = true; 147 break; 148 case '0': case '1': case '2': case '3': case '4': 149 case '5': case '6': case '7': case '8': case '9': 150 if (nonzero) 151 { 152 real x = ret + ret; 153 x = x + x + ret; 154 ret = x + x; 155 } 156 if (*p != '0') 157 { 158 ret += (int)(*p  '0'); 159 nonzero = true; 160 } 161 if (comma) 162 exponent; 163 break; 164 case 'e': 165 case 'E': 166 exponent += atoi(p + 1); 167 finished = true; 168 break; 169 default: 170 finished = true; 171 break; 172 } 173 } 174 175 if (exponent) 176 ret *= pow(R_10, (real)exponent); 177 178 if (negative) 179 ret = ret; 180 181 new(this) real(ret); 122 182 } 123 183 
trunk/src/real.h
r1082 r1115 37 37 real(int i); 38 38 real(unsigned int i); 39 40 real(char const *str); 39 41 40 42 operator float() const; 
trunk/test/unit/real.cpp
r1019 r1115 102 102 LOLUNIT_ASSERT_DOUBLES_EQUAL(a5, 1.5, 0.0); 103 103 LOLUNIT_ASSERT_DOUBLES_EQUAL(a6, 1234567876543210.0, 0.0); 104 } 105 106 LOLUNIT_TEST(StringToReal) 107 { 108 float a1 = real("0"); 109 float a2 = real("1"); 110 float a3 = real("1"); 111 /* 2^128 * 2^128 */ 112 float a4 = real("0.0000000000000000000000000000000000000029387358770" 113 "557187699218413430556141945466638919302188037718792" 114 "6569604314863681793212890625") 115 * real("340282366920938463463374607431768211456"); 116 117 LOLUNIT_ASSERT_EQUAL(a1, 0.0f); 118 LOLUNIT_ASSERT_EQUAL(a2, 1.0f); 119 LOLUNIT_ASSERT_EQUAL(a3, 1.0f); 120 LOLUNIT_ASSERT_EQUAL(a4, 1.0f); 104 121 } 105 122
Note: See TracChangeset
for help on using the changeset viewer.