## [MinGW-notify] [ mingw-Bugs-3536824 ] Problem with pow() function when calculating power of 10

 [MinGW-notify] [ mingw-Bugs-3536824 ] Problem with pow() function when calculating power of 10 From: SF/projects/mingw notification list - 2012-06-21 08:48:07 ```Bugs item #3536824, was opened at 2012-06-21 01:48 Message generated for change (Tracker Item Submitted) made by chirayu45 You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=102435&aid=3536824&group_id=2435 Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: gcc Group: None Status: Open Resolution: None Priority: 5 Private: No Submitted By: Chirayu Chiripal (chirayu45) Assigned to: Nobody/Anonymous (nobody) Summary: Problem with pow() function when calculating power of 10 Initial Comment: My program is calculating the log=log10(num) and then using pow(10,log-1) to get the power of 10 one less than its log. Here is the code: // Sandbox.c #include #include int main() { int t,n,log,po; printf(\"\\nTest Cases:\"); scanf(\"%d\",&t); while(t--) { printf(\"\\nNumber:\"); scanf(\"%d\",&n); log=log10(n); printf(\"\\nlog=%d\\n\",log); po=pow(10,log); printf(\"pow(10,log)=%d\\n\",po); po=pow(10,log-1); printf(\"pow(10,log-1)=%d\\n\",po); po=pow(10,log); printf(\"pow(10,log)=%d\\n\",po); po=pow(10,log)/10; printf(\"pow(10,log)/10=%d\\n\\n\",po); } return 0; } OUTPUT (Expected): Test Cases:2 Number:1020 log=3 pow(10,log)=1000 pow(10,log-1)=100 pow(10,log)=1000 pow(10,log)/10=100 Number:10230 log=4 pow(10,log)=10000 pow(10,log-1)=1000 pow(10,log)=10000 pow(10,log)/10=1000 OUTPUT (Actual): Test Cases:2 Number:1020 log=3 pow(10,log)=1000 pow(10,log-1)=99 // Problem Here pow(10,log)=1000 pow(10,log)/10=100 Number:10230 log=4 pow(10,log)=10000 pow(10,log-1)=1000 pow(10,log)=9999 // Problem Here pow(10,log)/10=999 // Problem Here Compiler Options Used in CMD: >gcc sandbox.c -o sand >sand Version shown by \"gcc -v\": Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/4.6.2/lto-wrapper.exe Target: mingw32 Configured with: ../gcc-4.6.2/configure --enable-languages=c,c++,ada,fortran,obj c,obj-c++ --disable-sjlj-exceptions --with-dwarf2 --enable-shared --enable-libgo mp --disable-win32-registry --enable-libstdcxx-debug --enable-version-specific-r untime-libs --build=mingw32 --prefix=/mingw Thread model: win32 gcc version 4.6.2 (GCC) System Specs: Win 7 Home Premium 64 bit Intel Core i7-2670QM @ 2.20 GHz Works fine when run on IDEONE in C(gcc-4.3.4) http://ideone.com/cQRrY ---------------------------------------------------------------------- You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=102435&aid=3536824&group_id=2435 ```

 [MinGW-notify] [ mingw-Bugs-3536824 ] Problem with pow() function when calculating power of 10 From: SF/projects/mingw notification list - 2012-06-21 08:48:07 ```Bugs item #3536824, was opened at 2012-06-21 01:48 Message generated for change (Tracker Item Submitted) made by chirayu45 You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=102435&aid=3536824&group_id=2435 Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: gcc Group: None Status: Open Resolution: None Priority: 5 Private: No Submitted By: Chirayu Chiripal (chirayu45) Assigned to: Nobody/Anonymous (nobody) Summary: Problem with pow() function when calculating power of 10 Initial Comment: My program is calculating the log=log10(num) and then using pow(10,log-1) to get the power of 10 one less than its log. Here is the code: // Sandbox.c #include #include int main() { int t,n,log,po; printf(\"\\nTest Cases:\"); scanf(\"%d\",&t); while(t--) { printf(\"\\nNumber:\"); scanf(\"%d\",&n); log=log10(n); printf(\"\\nlog=%d\\n\",log); po=pow(10,log); printf(\"pow(10,log)=%d\\n\",po); po=pow(10,log-1); printf(\"pow(10,log-1)=%d\\n\",po); po=pow(10,log); printf(\"pow(10,log)=%d\\n\",po); po=pow(10,log)/10; printf(\"pow(10,log)/10=%d\\n\\n\",po); } return 0; } OUTPUT (Expected): Test Cases:2 Number:1020 log=3 pow(10,log)=1000 pow(10,log-1)=100 pow(10,log)=1000 pow(10,log)/10=100 Number:10230 log=4 pow(10,log)=10000 pow(10,log-1)=1000 pow(10,log)=10000 pow(10,log)/10=1000 OUTPUT (Actual): Test Cases:2 Number:1020 log=3 pow(10,log)=1000 pow(10,log-1)=99 // Problem Here pow(10,log)=1000 pow(10,log)/10=100 Number:10230 log=4 pow(10,log)=10000 pow(10,log-1)=1000 pow(10,log)=9999 // Problem Here pow(10,log)/10=999 // Problem Here Compiler Options Used in CMD: >gcc sandbox.c -o sand >sand Version shown by \"gcc -v\": Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/4.6.2/lto-wrapper.exe Target: mingw32 Configured with: ../gcc-4.6.2/configure --enable-languages=c,c++,ada,fortran,obj c,obj-c++ --disable-sjlj-exceptions --with-dwarf2 --enable-shared --enable-libgo mp --disable-win32-registry --enable-libstdcxx-debug --enable-version-specific-r untime-libs --build=mingw32 --prefix=/mingw Thread model: win32 gcc version 4.6.2 (GCC) System Specs: Win 7 Home Premium 64 bit Intel Core i7-2670QM @ 2.20 GHz Works fine when run on IDEONE in C(gcc-4.3.4) http://ideone.com/cQRrY ---------------------------------------------------------------------- You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=102435&aid=3536824&group_id=2435 ```
 [MinGW-notify] [ mingw-Bugs-3536824 ] Problem with pow() function when calculating power of 10 From: SF/projects/mingw notification list - 2012-06-21 08:57:03 ```Bugs item #3536824, was opened at 2012-06-21 01:48 Message generated for change (Comment added) made by chirayu45 You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=102435&aid=3536824&group_id=2435 Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: gcc Group: None Status: Open Resolution: None Priority: 5 Private: No Submitted By: Chirayu Chiripal (chirayu45) Assigned to: Nobody/Anonymous (nobody) Summary: Problem with pow() function when calculating power of 10 Initial Comment: My program is calculating the log=log10(num) and then using pow(10,log-1) to get the power of 10 one less than its log. Here is the code: // Sandbox.c #include #include int main() { int t,n,log,po; printf(\"\\nTest Cases:\"); scanf(\"%d\",&t); while(t--) { printf(\"\\nNumber:\"); scanf(\"%d\",&n); log=log10(n); printf(\"\\nlog=%d\\n\",log); po=pow(10,log); printf(\"pow(10,log)=%d\\n\",po); po=pow(10,log-1); printf(\"pow(10,log-1)=%d\\n\",po); po=pow(10,log); printf(\"pow(10,log)=%d\\n\",po); po=pow(10,log)/10; printf(\"pow(10,log)/10=%d\\n\\n\",po); } return 0; } OUTPUT (Expected): Test Cases:2 Number:1020 log=3 pow(10,log)=1000 pow(10,log-1)=100 pow(10,log)=1000 pow(10,log)/10=100 Number:10230 log=4 pow(10,log)=10000 pow(10,log-1)=1000 pow(10,log)=10000 pow(10,log)/10=1000 OUTPUT (Actual): Test Cases:2 Number:1020 log=3 pow(10,log)=1000 pow(10,log-1)=99 // Problem Here pow(10,log)=1000 pow(10,log)/10=100 Number:10230 log=4 pow(10,log)=10000 pow(10,log-1)=1000 pow(10,log)=9999 // Problem Here pow(10,log)/10=999 // Problem Here Compiler Options Used in CMD: >gcc sandbox.c -o sand >sand Version shown by \"gcc -v\": Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/4.6.2/lto-wrapper.exe Target: mingw32 Configured with: ../gcc-4.6.2/configure --enable-languages=c,c++,ada,fortran,obj c,obj-c++ --disable-sjlj-exceptions --with-dwarf2 --enable-shared --enable-libgo mp --disable-win32-registry --enable-libstdcxx-debug --enable-version-specific-r untime-libs --build=mingw32 --prefix=/mingw Thread model: win32 gcc version 4.6.2 (GCC) System Specs: Win 7 Home Premium 64 bit Intel Core i7-2670QM @ 2.20 GHz Works fine when run on IDEONE in C(gcc-4.3.4) http://ideone.com/cQRrY ---------------------------------------------------------------------- >Comment By: Chirayu Chiripal (chirayu45) Date: 2012-06-21 01:57 Message: Also reported on GCC Bugzilla: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53738 They said "Works for me correctly on linux. In fact there is nothing GCC can do since pow is not provided by GCC but rather who ever provides your libc." ---------------------------------------------------------------------- You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=102435&aid=3536824&group_id=2435 ```
 [MinGW-notify] [ mingw-Bugs-3536824 ] Problem with pow() function when calculating power of 10 From: SF/projects/mingw notification list - 2012-06-21 09:01:34 ```Bugs item #3536824, was opened at 2012-06-21 01:48 Message generated for change (Comment added) made by chirayu45 You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=102435&aid=3536824&group_id=2435 Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: gcc Group: None Status: Open Resolution: None Priority: 5 Private: No Submitted By: Chirayu Chiripal (chirayu45) Assigned to: Nobody/Anonymous (nobody) Summary: Problem with pow() function when calculating power of 10 Initial Comment: My program is calculating the log=log10(num) and then using pow(10,log-1) to get the power of 10 one less than its log. Here is the code: // Sandbox.c #include #include int main() { int t,n,log,po; printf(\"\\nTest Cases:\"); scanf(\"%d\",&t); while(t--) { printf(\"\\nNumber:\"); scanf(\"%d\",&n); log=log10(n); printf(\"\\nlog=%d\\n\",log); po=pow(10,log); printf(\"pow(10,log)=%d\\n\",po); po=pow(10,log-1); printf(\"pow(10,log-1)=%d\\n\",po); po=pow(10,log); printf(\"pow(10,log)=%d\\n\",po); po=pow(10,log)/10; printf(\"pow(10,log)/10=%d\\n\\n\",po); } return 0; } OUTPUT (Expected): Test Cases:2 Number:1020 log=3 pow(10,log)=1000 pow(10,log-1)=100 pow(10,log)=1000 pow(10,log)/10=100 Number:10230 log=4 pow(10,log)=10000 pow(10,log-1)=1000 pow(10,log)=10000 pow(10,log)/10=1000 OUTPUT (Actual): Test Cases:2 Number:1020 log=3 pow(10,log)=1000 pow(10,log-1)=99 // Problem Here pow(10,log)=1000 pow(10,log)/10=100 Number:10230 log=4 pow(10,log)=10000 pow(10,log-1)=1000 pow(10,log)=9999 // Problem Here pow(10,log)/10=999 // Problem Here Compiler Options Used in CMD: >gcc sandbox.c -o sand >sand Version shown by \"gcc -v\": Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/4.6.2/lto-wrapper.exe Target: mingw32 Configured with: ../gcc-4.6.2/configure --enable-languages=c,c++,ada,fortran,obj c,obj-c++ --disable-sjlj-exceptions --with-dwarf2 --enable-shared --enable-libgo mp --disable-win32-registry --enable-libstdcxx-debug --enable-version-specific-r untime-libs --build=mingw32 --prefix=/mingw Thread model: win32 gcc version 4.6.2 (GCC) System Specs: Win 7 Home Premium 64 bit Intel Core i7-2670QM @ 2.20 GHz Works fine when run on IDEONE in C(gcc-4.3.4) http://ideone.com/cQRrY ---------------------------------------------------------------------- >Comment By: Chirayu Chiripal (chirayu45) Date: 2012-06-21 02:01 Message: Then i updated my GCC version to 4.7.0 but the problem was not resolved. GCC Version Now: Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/4.7.0/lto-wrapper.exe Target: mingw32 Configured with: ../gcc-4.7.0/configure --enable-languages=c,c++,ada,fortran,obj c,obj-c++ --disable-sjlj-exceptions --with-dwarf2 --enable-shared --enable-libgo mp --disable-win32-registry --enable-libstdcxx-debug --disable-build-poststage1- with-cxx --enable-version-specific-runtime-libs --build=mingw32 --prefix=/mingw Thread model: win32 gcc version 4.7.0 (GCC) Binutils Version: GNU ld (GNU Binutils) 2.22 ---------------------------------------------------------------------- Comment By: Chirayu Chiripal (chirayu45) Date: 2012-06-21 01:57 Message: Also reported on GCC Bugzilla: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53738 They said "Works for me correctly on linux. In fact there is nothing GCC can do since pow is not provided by GCC but rather who ever provides your libc." ---------------------------------------------------------------------- You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=102435&aid=3536824&group_id=2435 ```
 [MinGW-notify] [ mingw-Bugs-3536824 ] Problem with pow() function when calculating power of 10 From: SF/projects/mingw notification list - 2012-06-21 10:57:38 ```Bugs item #3536824, was opened at 2012-06-21 01:48 Message generated for change (Comment added) made by keithmarshall You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=102435&aid=3536824&group_id=2435 Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. >Category: non-mingw >Group: Known Feature >Status: Closed >Resolution: Rejected Priority: 5 Private: No Submitted By: Chirayu Chiripal (chirayu45) Assigned to: Nobody/Anonymous (nobody) Summary: Problem with pow() function when calculating power of 10 Initial Comment: My program is calculating the log=log10(num) and then using pow(10,log-1) to get the power of 10 one less than its log. Here is the code: // Sandbox.c #include #include int main() { int t,n,log,po; printf(\"\\nTest Cases:\"); scanf(\"%d\",&t); while(t--) { printf(\"\\nNumber:\"); scanf(\"%d\",&n); log=log10(n); printf(\"\\nlog=%d\\n\",log); po=pow(10,log); printf(\"pow(10,log)=%d\\n\",po); po=pow(10,log-1); printf(\"pow(10,log-1)=%d\\n\",po); po=pow(10,log); printf(\"pow(10,log)=%d\\n\",po); po=pow(10,log)/10; printf(\"pow(10,log)/10=%d\\n\\n\",po); } return 0; } OUTPUT (Expected): Test Cases:2 Number:1020 log=3 pow(10,log)=1000 pow(10,log-1)=100 pow(10,log)=1000 pow(10,log)/10=100 Number:10230 log=4 pow(10,log)=10000 pow(10,log-1)=1000 pow(10,log)=10000 pow(10,log)/10=1000 OUTPUT (Actual): Test Cases:2 Number:1020 log=3 pow(10,log)=1000 pow(10,log-1)=99 // Problem Here pow(10,log)=1000 pow(10,log)/10=100 Number:10230 log=4 pow(10,log)=10000 pow(10,log-1)=1000 pow(10,log)=9999 // Problem Here pow(10,log)/10=999 // Problem Here Compiler Options Used in CMD: >gcc sandbox.c -o sand >sand Version shown by \"gcc -v\": Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/4.6.2/lto-wrapper.exe Target: mingw32 Configured with: ../gcc-4.6.2/configure --enable-languages=c,c++,ada,fortran,obj c,obj-c++ --disable-sjlj-exceptions --with-dwarf2 --enable-shared --enable-libgo mp --disable-win32-registry --enable-libstdcxx-debug --enable-version-specific-r untime-libs --build=mingw32 --prefix=/mingw Thread model: win32 gcc version 4.6.2 (GCC) System Specs: Win 7 Home Premium 64 bit Intel Core i7-2670QM @ 2.20 GHz Works fine when run on IDEONE in C(gcc-4.3.4) http://ideone.com/cQRrY ---------------------------------------------------------------------- >Comment By: Keith Marshall (keithmarshall) Date: 2012-06-21 03:57 Message: Herewith, my comments: 1) The provider of your libc is Microsoft, (it's MSVCRT.DLL). 2) Your test case is invalid; I had to modify it to even get it to compile! 3) After removing all the invalid backslashes from your code, I can reproduce your result; however, I can equally easily correct the issue, getting correct results, by refactoring your code, to eliminate unsafe int --> double type coercions: \$ cat foo.c #include #include int main( int argc, char **argv ) { while( --argc ) { int n = atoi( *++argv ); int log = log10( n ); do { int res = pow( 10, log ); printf( "n = %d; log = %d; pow = %d\n", n, log, res ); } while( log-- ); } return 0; } \$ gcc -o foo foo.c \$ ./foo 1020 10230 n = 1020; log = 3; pow = 1000 n = 1020; log = 2; pow = 100 n = 1020; log = 1; pow = 10 n = 1020; log = 0; pow = 1 n = 10230; log = 4; pow = 10000 n = 10230; log = 3; pow = 1000 n = 10230; log = 2; pow = 100 n = 10230; log = 1; pow = 10 n = 10230; log = 0; pow = 1 4) There is a note about potential rounding (excess precision) errors in /mingw/include/math.h; you should read it. Given that this issue arises from improper use of Microsoft code, and is easily circumvented by writing your own code correctly, (i.e. avoiding unsafe implicit type coercions), we will not pursue this further. ---------------------------------------------------------------------- Comment By: Chirayu Chiripal (chirayu45) Date: 2012-06-21 02:01 Message: Then i updated my GCC version to 4.7.0 but the problem was not resolved. GCC Version Now: Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/4.7.0/lto-wrapper.exe Target: mingw32 Configured with: ../gcc-4.7.0/configure --enable-languages=c,c++,ada,fortran,obj c,obj-c++ --disable-sjlj-exceptions --with-dwarf2 --enable-shared --enable-libgo mp --disable-win32-registry --enable-libstdcxx-debug --disable-build-poststage1- with-cxx --enable-version-specific-runtime-libs --build=mingw32 --prefix=/mingw Thread model: win32 gcc version 4.7.0 (GCC) Binutils Version: GNU ld (GNU Binutils) 2.22 ---------------------------------------------------------------------- Comment By: Chirayu Chiripal (chirayu45) Date: 2012-06-21 01:57 Message: Also reported on GCC Bugzilla: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53738 They said "Works for me correctly on linux. In fact there is nothing GCC can do since pow is not provided by GCC but rather who ever provides your libc." ---------------------------------------------------------------------- You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=102435&aid=3536824&group_id=2435 ```