diff options
author | Mike Buland <eichlan@xagasoft.com> | 2015-01-29 08:56:43 -0700 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2015-01-29 08:56:43 -0700 |
commit | 14419813582bc89334d2e9d5bdd74b5852bfc9f9 (patch) | |
tree | 9b6dff56e27d485652dfd019391aef4845ff01c1 | |
parent | 42e77eb7ba24d2b7ebd47fadbc9240dc4520a5d6 (diff) | |
parent | f03026f5c2cde1eecfda273eaa52df10287f0f9e (diff) | |
download | clic-14419813582bc89334d2e9d5bdd74b5852bfc9f9.tar.gz clic-14419813582bc89334d2e9d5bdd74b5852bfc9f9.tar.bz2 clic-14419813582bc89334d2e9d5bdd74b5852bfc9f9.tar.xz clic-14419813582bc89334d2e9d5bdd74b5852bfc9f9.zip |
Merge branch 'master' of /home/eichlan/git/clic
Diffstat (limited to '')
-rw-r--r-- | src/config.h | 3 | ||||
-rw-r--r-- | src/main.cpp | 2 | ||||
-rw-r--r-- | src/number.cpp | 63 | ||||
-rw-r--r-- | src/number.h | 2 | ||||
-rw-r--r-- | src/options.cpp | 100 | ||||
-rw-r--r-- | src/options.h | 1 | ||||
-rw-r--r-- | src/parser.cpp | 1 | ||||
-rw-r--r-- | src/unitnumber.cpp | 12 | ||||
-rw-r--r-- | src/unitnumber.h | 1 | ||||
-rw-r--r-- | src/unitparser.cpp | 2 |
10 files changed, 161 insertions, 26 deletions
diff --git a/src/config.h b/src/config.h index a3f8b9e..482ef9e 100644 --- a/src/config.h +++ b/src/config.h | |||
@@ -1,11 +1,12 @@ | |||
1 | #ifndef CONFIG_H | 1 | #ifndef CONFIG_H |
2 | #define CONFIG_H | 2 | #define CONFIG_H |
3 | 3 | ||
4 | #define CLIC_VERSION "0.11" | 4 | #define CLIC_VERSION "0.13" |
5 | #define CLIC_VERSION_STR "Clic v" CLIC_VERSION | 5 | #define CLIC_VERSION_STR "Clic v" CLIC_VERSION |
6 | 6 | ||
7 | #define DEBUG_DIVIDE false | 7 | #define DEBUG_DIVIDE false |
8 | #define DEBUG_PARSE false | 8 | #define DEBUG_PARSE false |
9 | #define DEBUG_ADD false | ||
9 | 10 | ||
10 | #define DBS( s, x ) if( DEBUG_ ##s ) { x; } (void)0 | 11 | #define DBS( s, x ) if( DEBUG_ ##s ) { x; } (void)0 |
11 | #define DBS_START( s ) if( DEBUG_ ##s ) { (void)0 | 12 | #define DBS_START( s ) if( DEBUG_ ##s ) { (void)0 |
diff --git a/src/main.cpp b/src/main.cpp index a1d7672..1a4ab38 100644 --- a/src/main.cpp +++ b/src/main.cpp | |||
@@ -8,6 +8,8 @@ | |||
8 | #include <math.h> | 8 | #include <math.h> |
9 | #include <sys/time.h> | 9 | #include <sys/time.h> |
10 | 10 | ||
11 | #include <bu/file.h> | ||
12 | |||
11 | #include <bu/sio.h> | 13 | #include <bu/sio.h> |
12 | #include <bu/streamstack.h> | 14 | #include <bu/streamstack.h> |
13 | using namespace Bu; | 15 | using namespace Bu; |
diff --git a/src/number.cpp b/src/number.cpp index 05e310c..660ba61 100644 --- a/src/number.cpp +++ b/src/number.cpp | |||
@@ -610,6 +610,19 @@ int Number::digit( int iIdx ) const | |||
610 | return aInt[iIdx]; | 610 | return aInt[iIdx]; |
611 | } | 611 | } |
612 | 612 | ||
613 | void Number::setDigit( int iIdx, int iVal ) | ||
614 | { | ||
615 | if( iIdx < 0 ) | ||
616 | { | ||
617 | if( iIdx >= -iScale ) | ||
618 | aFrac.set( -1-iIdx, iVal ); | ||
619 | return; | ||
620 | } | ||
621 | if( iIdx >= aInt.getSize() ) | ||
622 | return; | ||
623 | aInt.set( iIdx, iVal ); | ||
624 | } | ||
625 | |||
613 | void Number::setScale( int iNewScale ) | 626 | void Number::setScale( int iNewScale ) |
614 | { | 627 | { |
615 | if( iNewScale == 0 ) | 628 | if( iNewScale == 0 ) |
@@ -662,11 +675,11 @@ Number Number::toRadix( int iNewRadix, int iNewScale ) const | |||
662 | n.set( iNewRadix ); | 675 | n.set( iNewRadix ); |
663 | while( !me.isZero() ) | 676 | while( !me.isZero() ) |
664 | { | 677 | { |
665 | Bu::println("%1 %% %2 = %3").arg( me ).arg( n ).arg( me%n ); | 678 | // Bu::println("%1 %% %2 = %3").arg( me ).arg( n ).arg( me%n ); |
666 | int dig = (me%n).toInt32(); | 679 | int dig = (me%n).toInt32(); |
667 | ret.aInt.append( dig ); | 680 | ret.aInt.append( dig ); |
668 | me = me / n; | 681 | me = me / n; |
669 | Bu::println("New digit: %1 (%2)").arg( dig ).arg( me ); | 682 | // Bu::println("New digit: %1 (%2)").arg( dig ).arg( me ); |
670 | } | 683 | } |
671 | 684 | ||
672 | return ret; | 685 | return ret; |
@@ -680,7 +693,7 @@ Number Number::add( const Number &rhs, bool bSub ) const | |||
680 | 693 | ||
681 | int iZeros = 0; | 694 | int iZeros = 0; |
682 | int iCarry = 0; | 695 | int iCarry = 0; |
683 | // Bu::println("::: %1 + %2:").arg( toString() ).arg( rhs.toString() ); | 696 | DBS( ADD, Bu::println("::: %1 + %2:").arg( toString() ).arg( rhs.toString() ) ); |
684 | 697 | ||
685 | if( bPositive == (bSub?!rhs.bPositive:rhs.bPositive)) | 698 | if( bPositive == (bSub?!rhs.bPositive:rhs.bPositive)) |
686 | { | 699 | { |
@@ -688,9 +701,10 @@ Number Number::add( const Number &rhs, bool bSub ) const | |||
688 | for( int j = -iScale; j < iPlaces || iCarry > 0; j++ ) | 701 | for( int j = -iScale; j < iPlaces || iCarry > 0; j++ ) |
689 | { | 702 | { |
690 | int iRes = iCarry + digit( j ) + rhs.digit( j ); | 703 | int iRes = iCarry + digit( j ) + rhs.digit( j ); |
691 | // Bu::println(" [%5] Place: %1 + %2 + (%3) = %4"). | 704 | DBS( ADD, |
692 | // arg( digit(j) ).arg( rhs.digit( j ) ).arg( iCarry ) | 705 | Bu::println(" [%5] Place: %1 + %2 + (%3) = %4"). |
693 | // .arg( iRes ).arg( j ); | 706 | arg( digit(j) ).arg( rhs.digit( j ) ).arg( iCarry ) |
707 | .arg( iRes ).arg( j ) ); | ||
694 | if( j < 0 ) | 708 | if( j < 0 ) |
695 | ret.aFrac.set( -1-j, (iRes%iRadix) ); | 709 | ret.aFrac.set( -1-j, (iRes%iRadix) ); |
696 | else | 710 | else |
@@ -714,16 +728,25 @@ Number Number::add( const Number &rhs, bool bSub ) const | |||
714 | else | 728 | else |
715 | { | 729 | { |
716 | iCarry = 1; | 730 | iCarry = 1; |
717 | // Bu::println("--method b--"); | 731 | DBS( ADD, Bu::println("--method b--") ); |
718 | ret.bPositive = bPositive; | 732 | ret.bPositive = bPositive; |
719 | int iRes; | 733 | int iRes; |
720 | int iComp = (iRadix-1); | 734 | int iComp = (iRadix-1); |
735 | |||
721 | for( int j = -iScale; j < iPlaces; j++ ) | 736 | for( int j = -iScale; j < iPlaces; j++ ) |
722 | { | 737 | { |
723 | iRes = digit( j ) + (iComp-rhs.digit( j )) + iCarry; | 738 | iRes = digit( j ) + (iComp-rhs.digit( j )) + iCarry; |
724 | // Bu::println(" Place: %1 + %2 + (%3) = %4"). | 739 | if( iRes < iRadix ) |
725 | // arg( digit(j) ).arg( 9-rhs.digit( j ) ).arg( iCarry ) | 740 | { |
726 | // .arg( iRes ); | 741 | iCarry = 0; |
742 | } | ||
743 | else | ||
744 | iCarry = iRes/iRadix; | ||
745 | |||
746 | DBS( ADD, | ||
747 | Bu::println(" Place: %1 + %2 + (%3) = %4"). | ||
748 | arg( digit(j) ).arg( iComp-rhs.digit( j ) ).arg( iCarry ) | ||
749 | .arg( iRes ) ); | ||
727 | if( j < 0 ) | 750 | if( j < 0 ) |
728 | ret.aFrac.set( -1-j, (iRes%iRadix) ); | 751 | ret.aFrac.set( -1-j, (iRes%iRadix) ); |
729 | else | 752 | else |
@@ -738,24 +761,22 @@ Number Number::add( const Number &rhs, bool bSub ) const | |||
738 | 761 | ||
739 | ret.aInt.append( (iRes%iRadix) ); | 762 | ret.aInt.append( (iRes%iRadix) ); |
740 | } | 763 | } |
741 | if( iRes < iRadix ) | ||
742 | iCarry = 0; | ||
743 | else | ||
744 | iCarry = iRes/iRadix; | ||
745 | } | 764 | } |
746 | if( iCarry == 0 ) | 765 | if( iCarry == 0 ) |
747 | { | 766 | { |
748 | ret.bPositive = false; | 767 | ret.bPositive = false; |
749 | ret.aInt.set( 0, iComp-ret.aInt.get( 0 )+1); | 768 | int iVal = iComp-ret.digit( -ret.iScale )+1; |
750 | for( int j = 1; j < ret.aInt.getSize(); j++ ) | 769 | iCarry = (iVal >= iRadix) ? 1 : 0; |
770 | ret.setDigit( -ret.iScale, iVal%iRadix ); | ||
771 | |||
772 | for( int j = -iScale+1; j < ret.aInt.getSize(); j++ ) | ||
751 | { | 773 | { |
752 | ret.aInt.set( j, iComp-ret.aInt.get( j )); | 774 | iVal = iComp-ret.digit( j )+iCarry; |
775 | iCarry = (iVal >= iRadix) ? 1 : 0; | ||
776 | ret.setDigit( j, iVal%iRadix ); | ||
753 | } | 777 | } |
754 | } | 778 | } |
755 | else | 779 | ret.aInt.trim(); |
756 | { | ||
757 | ret.aInt.trim(); | ||
758 | } | ||
759 | } | 780 | } |
760 | 781 | ||
761 | return ret; | 782 | return ret; |
diff --git a/src/number.h b/src/number.h index fe10788..79cd5f5 100644 --- a/src/number.h +++ b/src/number.h | |||
@@ -45,10 +45,12 @@ public: | |||
45 | Bu::String toString() const; | 45 | Bu::String toString() const; |
46 | 46 | ||
47 | int digit( int iIdx ) const; | 47 | int digit( int iIdx ) const; |
48 | void setDigit( int iIdx, int iVal ); | ||
48 | 49 | ||
49 | int getScale() const { return iScale; } | 50 | int getScale() const { return iScale; } |
50 | int getEffectiveScale() const; | 51 | int getEffectiveScale() const; |
51 | void setScale( int iNewScale ); | 52 | void setScale( int iNewScale ); |
53 | int getIntegralDigits() const { return aInt.getSize(); } | ||
52 | 54 | ||
53 | int32_t toInt32() const; | 55 | int32_t toInt32() const; |
54 | Number toRadix( int iNewRadix, int iNewScale=-1 ) const; | 56 | Number toRadix( int iNewRadix, int iNewScale=-1 ) const; |
diff --git a/src/options.cpp b/src/options.cpp index 0633c26..c127022 100644 --- a/src/options.cpp +++ b/src/options.cpp | |||
@@ -40,6 +40,8 @@ Options::Options( int argc, char *argv[] ) : | |||
40 | "your shell)."); | 40 | "your shell)."); |
41 | addOption( Bu::slot(this, &Options::sum), "sum", | 41 | addOption( Bu::slot(this, &Options::sum), "sum", |
42 | "Read numbers from standard input and sum them, output the result."); | 42 | "Read numbers from standard input and sum them, output the result."); |
43 | addOption( Bu::slot(this, &Options::grind), 'g', "grind", | ||
44 | "Search for magic numbers. Secret magic numbers."); | ||
43 | addOption( Bu::slot(this, &Options::version), 'v', "version", | 45 | addOption( Bu::slot(this, &Options::version), 'v', "version", |
44 | "Show the version string ('" CLIC_VERSION_STR "')"); | 46 | "Show the version string ('" CLIC_VERSION_STR "')"); |
45 | addHelpOption('h', "help", "This help"); | 47 | addHelpOption('h', "help", "This help"); |
@@ -225,3 +227,101 @@ int Options::version( Bu::StringArray aArgs ) | |||
225 | return 0; | 227 | return 0; |
226 | } | 228 | } |
227 | 229 | ||
230 | bool onlyNines( const Bu::String &s ) | ||
231 | { | ||
232 | for( Bu::String::const_iterator i = s.begin(); i; i++ ) | ||
233 | { | ||
234 | if( *i != '9' ) | ||
235 | return false; | ||
236 | } | ||
237 | return true; | ||
238 | } | ||
239 | |||
240 | bool onlyOnes( const Bu::String &s ) | ||
241 | { | ||
242 | for( Bu::String::const_iterator i = s.begin(); i; i++ ) | ||
243 | { | ||
244 | if( *i != '1' ) | ||
245 | return false; | ||
246 | } | ||
247 | return true; | ||
248 | } | ||
249 | |||
250 | int Options::grind( Bu::StringArray aArgs ) | ||
251 | { | ||
252 | Number test("1"); | ||
253 | Number bit("1"); | ||
254 | Number mult("2"); | ||
255 | int64_t iBits = 1; | ||
256 | int iLastSize = 0; | ||
257 | |||
258 | if( aArgs.getSize() > 1 ) | ||
259 | { | ||
260 | Bu::print("Loading number..."); Bu::sioRaw.flush(); | ||
261 | Bu::String num = Bu::File(aArgs[1], Bu::File::Read).readAll(); | ||
262 | test = num; | ||
263 | Bu::println("done."); | ||
264 | Bu::print("Computing bit..."); Bu::sioRaw.flush(); | ||
265 | bit = test; | ||
266 | bit = bit+Number("1"); | ||
267 | bit = bit/mult; | ||
268 | Bu::println("done."); | ||
269 | if( aArgs.getSize() > 2 ) | ||
270 | { | ||
271 | Bu::print("Trusting a human for where we are..."); Bu::sioRaw.flush(); | ||
272 | iBits = strtoll( aArgs[2].getStr(), NULL, 10 ); | ||
273 | } | ||
274 | else | ||
275 | { | ||
276 | Bu::print("Figuring out where we are..."); Bu::sioRaw.flush(); | ||
277 | Bu::String sBin = bit.toRadix(2).toString(); | ||
278 | iBits = sBin.getSize(); | ||
279 | } | ||
280 | // Bu::println("Done. Checking."); | ||
281 | // Bu::println("test = %1, bit = %2").arg( test ).arg( bit ); | ||
282 | // Bu::String sTest = test.toRadix(2).toString(); | ||
283 | // Bu::File("check-bit.txt", Bu::File::WriteNew ).write( sBin ); | ||
284 | // Bu::File("check-test.txt", Bu::File::WriteNew ).write( sTest ); | ||
285 | iLastSize = num.getSize(); | ||
286 | // Bu::println("File written. %1 bits").arg( sBin.getSize() ); | ||
287 | Bu::println("done."); | ||
288 | Bu::println("Continuing calculation..."); | ||
289 | } | ||
290 | |||
291 | for(;;) | ||
292 | { | ||
293 | bit = bit*mult; | ||
294 | test = test + bit; | ||
295 | Bu::String r = test.toString(); | ||
296 | iBits++; | ||
297 | |||
298 | if( onlyNines(r) ) | ||
299 | { | ||
300 | Bu::println("%1 digits").arg( r.getSize() ); | ||
301 | Bu::File f("answer-dec.txt", Bu::File::WriteNew ); | ||
302 | f.write( r ); | ||
303 | Bu::println("Success."); | ||
304 | // Bu::println("%1 == %2").arg( test.toRadix(2) ).arg( r ); | ||
305 | return 0; | ||
306 | } | ||
307 | if( r.getSize() != iLastSize && r.getSize()%1000 == 0 ) | ||
308 | { | ||
309 | /* | ||
310 | Bu::String rs = test.toRadix(2).toString(); | ||
311 | int iOBits = rs.getSize(); | ||
312 | if( iOBits != iBits ) | ||
313 | Bu::println("Bit mismatch: %1 vs %2").arg( iOBits ).arg( iBits ); | ||
314 | if( !onlyOnes( rs ) ) | ||
315 | Bu::println("Not only ones!"); | ||
316 | */ | ||
317 | iLastSize = r.getSize(); | ||
318 | Bu::println("%1 digits").arg( r.getSize() ); | ||
319 | Bu::File f(Bu::String("interum-%1-%2.txt").arg( r.getSize() ).arg( iBits ), Bu::File::WriteNew ); | ||
320 | f.write( r ); | ||
321 | } | ||
322 | } | ||
323 | Bu::println("Failure."); | ||
324 | exit( 0 ); | ||
325 | return 0; | ||
326 | } | ||
327 | |||
diff --git a/src/options.h b/src/options.h index f3f59a6..a7453ab 100644 --- a/src/options.h +++ b/src/options.h | |||
@@ -20,6 +20,7 @@ private: | |||
20 | int execute( Bu::StringArray aArgs ); | 20 | int execute( Bu::StringArray aArgs ); |
21 | int sum( Bu::StringArray aArgs ); | 21 | int sum( Bu::StringArray aArgs ); |
22 | int version( Bu::StringArray aArgs ); | 22 | int version( Bu::StringArray aArgs ); |
23 | int grind( Bu::StringArray aArgs ); | ||
23 | 24 | ||
24 | int iScale; | 25 | int iScale; |
25 | int iRadix; | 26 | int iRadix; |
diff --git a/src/parser.cpp b/src/parser.cpp index bd49c38..153b6fe 100644 --- a/src/parser.cpp +++ b/src/parser.cpp | |||
@@ -270,6 +270,7 @@ void Parser::unwind() | |||
270 | { | 270 | { |
271 | 271 | ||
272 | tsNonTerminal.pop(); | 272 | tsNonTerminal.pop(); |
273 | unwind(); | ||
273 | } | 274 | } |
274 | else | 275 | else |
275 | { | 276 | { |
diff --git a/src/unitnumber.cpp b/src/unitnumber.cpp index 985f0d9..d213ed7 100644 --- a/src/unitnumber.cpp +++ b/src/unitnumber.cpp | |||
@@ -136,13 +136,17 @@ void UnitNumber::number1() | |||
136 | 136 | ||
137 | #define mathTest( anum, op, bnum, scale, answ ) \ | 137 | #define mathTest( anum, op, bnum, scale, answ ) \ |
138 | unitTest( (Number(anum, scale) op Number(bnum, scale)).toString() == answ ) | 138 | unitTest( (Number(anum, scale) op Number(bnum, scale)).toString() == answ ) |
139 | /* | 139 | |
140 | void UnitNumber::number2() | 140 | void UnitNumber::number2() |
141 | { | 141 | { |
142 | mathTest( "1", /, "17", 10, "0.0588235294" ); | 142 | mathTest("55", +, "-100", 0, "-45"); |
143 | mathTest( "1", /, "177", 10, "0.0056497175" ); | 143 | mathTest("30.01", +, "-31.21", 2, "-1.20"); |
144 | mathTest("55", -, "100", 0, "-45"); | ||
145 | mathTest("30.01", -, "31.21", 2, "-1.20"); | ||
146 | // mathTest( "1", /, "17", 10, "0.0588235294" ); | ||
147 | // mathTest( "1", /, "177", 10, "0.0056497175" ); | ||
144 | } | 148 | } |
145 | */ | 149 | |
146 | 150 | ||
147 | /* | 151 | /* |
148 | // clic | 152 | // clic |
diff --git a/src/unitnumber.h b/src/unitnumber.h index 96d882e..097fd5b 100644 --- a/src/unitnumber.h +++ b/src/unitnumber.h | |||
@@ -14,6 +14,7 @@ public: | |||
14 | void multiply1(); | 14 | void multiply1(); |
15 | void divide1(); | 15 | void divide1(); |
16 | void number1(); | 16 | void number1(); |
17 | void number2(); | ||
17 | void compare1(); | 18 | void compare1(); |
18 | void radix1(); | 19 | void radix1(); |
19 | void fraction1(); | 20 | void fraction1(); |
diff --git a/src/unitparser.cpp b/src/unitparser.cpp index e23f76c..f518fdc 100644 --- a/src/unitparser.cpp +++ b/src/unitparser.cpp | |||
@@ -38,6 +38,8 @@ void UnitParser::order1() | |||
38 | unitTest(parse("1.59*40/24*21", 5) == "55.65"); | 38 | unitTest(parse("1.59*40/24*21", 5) == "55.65"); |
39 | unitTest(parse("1.59*40/(24*21)", 5) == "0.12619"); // bc says it's this: "0.12619"); | 39 | unitTest(parse("1.59*40/(24*21)", 5) == "0.12619"); // bc says it's this: "0.12619"); |
40 | unitTest(parse("10+2*2*2+2") == "20"); | 40 | unitTest(parse("10+2*2*2+2") == "20"); |
41 | unitTest(parse("5-5-1") == "-1"); | ||
42 | unitTest(parse("5-(1+2+2)-1") == "-1"); | ||
41 | } | 43 | } |
42 | 44 | ||
43 | void UnitParser::assignment() | 45 | void UnitParser::assignment() |