User account creation filtered due to spam.

Bug 53738 - Problem with pow() function when calculating power of 10
Summary: Problem with pow() function when calculating power of 10
Status: RESOLVED MOVED
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 4.6.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-06-21 03:06 UTC by Chirayu Chiripal
Modified: 2012-06-21 03:33 UTC (History)
0 users

See Also:
Host:
Target: i?86-*-mingw32
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
Screenshot of the Bug (465.08 KB, image/png)
2012-06-21 03:06 UTC, Chirayu Chiripal
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Chirayu Chiripal 2012-06-21 03:06:09 UTC
Created attachment 27671 [details]
Screenshot of the Bug

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<stdio.h>
#include<math.h>
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 1 Chirayu Chiripal 2012-06-21 03:26:13 UTC
Also Tried compiling it with:

gcc -Wall -Wextra
and
-fno-strict-aliasing -fwrapv

but the result was same.
Comment 2 Andrew Pinski 2012-06-21 03:33:23 UTC
Works for me correctly on linux:
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

>Works fine when run on IDEONE in C(gcc-4.3.4)

That is most likely linux too.

In fact there is nothing GCC can do since pow is not provided by GCC but rather who ever provides your libc.  I would report this to the mingw folks.