![]() |
![]() |
#1 |
Registered User
Join Date: Apr 2009
Location: N/A
Posts: 904
|
Why pow() is giving me this result?
(using vbcc in case this might be important for the question).
I am trying to calculate the "power of" and print it on screen. However for some reason I get a different result if I print the result of the pow() directly or store the result of pow() in double variable and print the variable value. Here is the code: Code:
double base, power, result; base = 2; power = 2; result = pow(base,power); printf("a: %.2lf b: %.2lf\n", result, pow(base,power)); Code:
a: 11-5./0400.00 b:4.01 In order to compile the solution I added the -lmieee flag. Would anyone know what is happening? When I try the same code using an online C compiler I get the expected result (a: 4.00 b: 4.00). |
![]() |
![]() |
#2 |
Registered User
![]() Join Date: Dec 2010
Location: Athens/Greece
Age: 52
Posts: 702
|
You are using %lf in printf, try with %f
See for example this https://stackoverflow.com/a/4264154/8390560 |
![]() |
![]() |
#3 | |
This cat is no more
![]() Join Date: Dec 2004
Location: FRANCE
Age: 51
Posts: 7,349
|
Quote:
Check how pow is defined in your case. Is it returning a "float" or a "double"? if it returns a float, it's not going to end well with printf as %lf format is double (assigning in a double before hand solves the issue) |
|
![]() |
![]() |
#4 | |
Registered User
Join Date: Apr 2009
Location: N/A
Posts: 904
|
Thank you alkis, doing this gives the following result:
Code:
a: 1074790400.00 b: 4.00 So this leaves only the question on why storing the pow() result in a variable of type double prints something completely different than printing the result of pow() directly? Quote:
|
|
![]() |
![]() |
#5 |
Registered User
Join Date: Jan 2002
Location: Germany
Posts: 6,818
|
Did you #include <math.h>?
All those float/double functions only work correctly with proper prototypes. By default all parameters and results of functions are expected to be int and the compiler converts to/from float where it should not. It's even worse with double. The compiler converts to int and pushes 32 bits to the stack where the function expects a 64bit double. Only with prototyped functions the compiler knows what to do. |
![]() |
![]() |
#6 |
Registered User
Join Date: Apr 2009
Location: N/A
Posts: 904
|
I changed the code to float but still get same result:
Code:
float base, power, result; base = 2; power = 2; result = pow(base,power); printf("a: %.2f b: %.2f\n", result, pow(base,power)); Code:
a: 1074790400.00 b: 4.00 |
![]() |
![]() |
#7 | |
Registered User
Join Date: Apr 2009
Location: N/A
Posts: 904
|
I missed that!
Confirm that using either double or float is giving correct result when I add the #include <math.h>. Quote:
|
|
![]() |
![]() |
#8 |
botcher
![]() Join Date: Jun 2016
Location: Hamburg/Germany
Posts: 657
|
well, it should print
a: 4.00 b: 4.00 even without any includes. That should only cause warnings... |
![]() |
![]() |
#9 |
This cat is no more
![]() Join Date: Dec 2004
Location: FRANCE
Age: 51
Posts: 7,349
|
If you don't include math.h, then the compiler assumes (after a shitload of warnings)
Code:
int pow(int a,int b); don't leave any warnings in your code, that's a golden rule (that most software don't respect and get hell out of it) |
![]() |
![]() |
#10 | |
Registered User
Join Date: Apr 2009
Location: N/A
Posts: 904
|
To be fair I only started to get a warning when I used pow() directly in the printf and was ignorning it as had only added that in the printf to quick "debug" what was happening. The following does not give any warnings when not including #include <math.h> but the result is still wrong.
Code:
double result = pow(base,power); printf("result: %.2f\n", result); Code:
result: 1074790400.00 Quote:
|
|
![]() |
![]() |
#11 |
Registered User
Join Date: Jan 2002
Location: Germany
Posts: 6,818
|
|
![]() |
![]() |
#12 | |
Natteravn
![]() Join Date: Nov 2009
Location: Herford / Germany
Posts: 2,347
|
As far as I can see the behaviour shown here is absolutely to be expected.
All caused by missing the prototype for pow()! ![]() No, it shouldn't. At least not according to ISO-C. Maybe gcc recognizes some math functions automatically? But then it certainly doesn't work for new floating point functions. Quote:
Code:
frank@altair cat tst.c int main() { double base = 2; double power = 2; double result = pow(base,power); printf("result: %.2f\n", result); return 0; } frank@altair vc +aos68k -o tst tst.c -lmieee > double result = pow(base,power); warning 161 in line 5 of "tst.c": implicit declaration of function <pow> > printf("result: %.2f\n", result); warning 161 in line 6 of "tst.c": implicit declaration of function <printf> > printf("result: %.2f\n", result); warning 213 in line 6 of "tst.c": varargs function called without prototype in scope |
|
![]() |
![]() |
#13 | ||
Registered User
Join Date: Apr 2009
Location: N/A
Posts: 904
|
Quote:
Quote:
What are the suggested -dontwarn values to use? Just adding -warn=-1 floods the compiler with warnings and many (sorry if saying so sound blasphemes ![]() Code:
struct Process *myProcess = NULL; no deceleration of global variable <myProcess> before definition. ... "dos/notify.h": member <nr_Reserved> does not have natural alignment. |
||
![]() |
![]() |
#14 | |||
Natteravn
![]() Join Date: Nov 2009
Location: Herford / Germany
Posts: 2,347
|
Quote:
Quote:
Sometimes you want to use -dontwarn=for compiling bad source texts, or very old ones using K&R syntax, before you have to fix everything in it. For you own sources you should never suppress warnings, but fix them. Quote:
![]() Just use the default settings. |
|||
![]() |
![]() |
#15 |
Registered User
![]() Join Date: Feb 2017
Location: Denmark
Posts: 571
|
As already mentioned it's highly adviceable to at least use the default warning level with your compiler. I usually go further, like thomas, and enable nearly every warning and then selectively disable the ones I don't want. Another tip is to make sure (most of) your code can be compiled with multiple compilers as they often catch different things.
VBCC is of course conformant here, and it's a classic mistake that's even covered by the comp.lang.c FAQ. (Not that I blame OP, I'm sure we've all made it) Sidenote MSVC (19.32) outputs 0.00/0.00 when compiling without optimizations but 4.0/4.0 with for the original code indication "special knowledge" of certain functions (as allowed by the standard). |
![]() |
![]() |
#16 |
Registered User
Join Date: Apr 2009
Location: N/A
Posts: 904
|
I have written this code in a separate solution and the only warning I am getting is in relation to vargs.
![]() My code is as follows: Code:
int main() { double base = 2; double power = 2; double result = pow(base,power); printf("result: %.2f\n", result); return 0; } Code:
CC=vc +aos68k COPT = LIBS = -lamiga -lauto -lmieee %.o : %.c $(CC) $(COPT) -c $< -o $@ HelloWorld : main.o $(CC) $^ -o $@ $(LIBS) main.o: main.h I have tested this on vbcc version vbcc 0.904 (30.12.2014). Since this looks old I installed the latest one I could find on aminet, however, also on vbcc 0.907 (03.10.2019) I do not get any warning when using pow() without prototype. Is something wrong with my setup? Everything compiles and I do get a warning, but not this particular one in relation to pow() |
![]() |
![]() |
#17 | |
Natteravn
![]() Join Date: Nov 2009
Location: Herford / Germany
Posts: 2,347
|
Quote:
Seems that they were made the default only with this year's release, V0.9h. So you may either want to update, or use -warn=161to enable it in older versions. |
|
![]() |
![]() |
#18 |
Registered User
Join Date: Apr 2009
Location: N/A
Posts: 904
|
Wanted to thank everyone for all the help. Have installed latest version and am getting some warnings in other areas which I am looking at. Many thanks
![]() |
![]() |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Automatic scaling gives different result depending on palette | thomas | support.WinUAE | 1 | 23 December 2016 20:18 |
Bought 1200 most disks result in guru meditation | ramonsmits | support.Hardware | 10 | 10 November 2015 20:26 |
Which one would likely result in potentially better performance? | Maren | support.WinUAE | 3 | 02 January 2011 08:32 |
Result!!! | wilshy | Retrogaming General Discussion | 6 | 22 October 2009 20:27 |
WinUAE/AIB - Apps result in black screen | Unregistered | New to Emulation or Amiga scene | 2 | 16 December 2001 20:31 |
|
|