I understand that this is a common problem. However I can't find a solid straight answer.
16 ^ 54 = 1.0531229167e+65 (this is the result I want)
When I use pow(16,54), I get:
105312291668557186697918027683670432318895095400549111254310977536.0
Code is as follows:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void main(){
double public;
double a = 16;
double b = 54;
public = (pow(a,b));
printf("%.21f\n", public);
}
Code executed with:
gcc main.c -lm
What I'm doing wrong?
What am I doing wrong?
Several things:
Use %.10e format for scientific notation with printf for a printout with ten digits after the dot,
Return an int from your main,
Consider not using public to name a variable, on the chance that your program would need to be ported to C++, where public is a keyword.
Here is how you can fix your program:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main(){
double p;
double a = 16;
double b = 54;
p = (pow(a,b));
printf("%.10e\n", p);
return 0;
}
Demo on ideone.
Have you tried:
printf("%e\n", public);
The %e specifier is for scientific notation, as described in the documentation
If you need scientific notation you need to use the %e format specifier:
printf("%e\n", public);
^^
Also, public is a keyword in C++ and so it would be a good idea to avoid that and any other keywords in case this code needs to be portable.
Related
i Have this homework about a superhero triangle, from what i got if the Area an integer data, then it's a superhero triangle, if it's not then it's not a superhero triangle.
The problem is, how do i determine if the output an integer or not
Thank you
Note : English is not my main language, sorry for the lack of vocabulary to describe it
Well I believe your calculation is all with doubles, because you are not sure of the result maybe an int maybe double. but you just need to know if it's really a double or an int, in your case 2.0 is an int of course, so I made this simple example for you
#include <iostream>
int isFloat(double n) {
return n - int(n) > 0;
}
int main(void) {
double x = 2.1;
if(isFloat(x)) {
std::cout << "it's a float";
}else {
std::cout << "it's an int";
}
return 0;
}
output
it's a float
Assuming you are just trying to check if a number is an integer (not necessarily the type but just that it has no decimal values... ex: 5.00 is an integer)
You could use the math.h standard library like this:
#include <stdio.h>
#include <math.h>
int main()
{
double output_value = 1.27;
if(fmod(output_value, 1.00) != 0)
//This is not an integer
else
//This is an integer
}
Clearly, if a number is not evenly divisible by 1, it is not an integer.
This seems like the simplest solution to me.
I suppose I am just stupid (I tried all). Help me out of here:
#include <stdio.h>
#include <string.h>
int main()
{
const char * y = "747.0";
double x;
x = atof(y);
printf("%.5f\n",x);
printf("%e\n",x);
return 0;
}
result:
0.00000
0.000000e+00
You need to include stdlib.h to offer atof() a prototype, without proper prototype, compiler will suppose its return value be an int.
Try including stdlib.h because I think that is the package you need for atoi in C.
I have a this code:
#include <stdio.h>
#include <conio.h>
void main()
{
int n = 5;
clrscr();
printf("n=%*d", n);
getch();
}
The output which I got is: n= 5. Why is there a space? How is it generated? What is the use of * in the code?
When in doubt, read the docs:
*:
The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
However, you appear to be using it wrong. The proper way to use it would be like this:
printf("n=%*d", 2, n);
It is clearly mentioned in the C Manual.
The answer is already given by Richard J. Ross III. Just quoting again what is said from the manual.
The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
Consider this code:
#include<stdio.h>
main()
{
int a,b;
float c,d;
a = 15;
b = a / 2;
printf("%d\n",b);
printf("%3d\n",b);
printf("%03d\n",b);
c = 15.3;
d = c / 3;
printf("%3.2f\n",d);
}
The output would be:
7
7
007
5.10
You can see here, how the printf function can be used for formatting output. Hope it helps. :)
With this *, you can set the width of your print with a variable.
I'm writing a function that should parse a string containing a description of a dice roll, for instance "2*1d8+2". I extract the four values OK when they are integers, but I want to be able to use floats as well for the multiplier and the addition at the end. Things get nasty when I try to parse such a string: "1.8*1d8+2.5".
I have determined that the problem is with the function strcspn. I ask it to parse the input string s (which contains the dice string) and stop at either an asterisk or an 'x':
const char * s = "1.8*1d8+2.5";
size_t l = strcspn(s,"*x");
The function should return 3, as the asterisk is at the 4th position. However, it seems to stop on the decimal separator (period) and returns 1.
It's not that I can't continue writing my function without this, as there are other ways to get things done, but still I'm curious why such a thing would happen. Has anyone ever encountered this problem before?
[EDIT]
Nevermind, I've found the answer, and it was my stupidity rather than the compiler playing tricks on me. I used this code:
if (l = strcspn(s,"*x") < strlen(s)) {
...
which returned 1 (or true) because strcspn(s,"*x") < strlen(s) evaluates to true - and was assigned to the l variable. I should have added parentheses:
if ((l = strcspn(s,"*x")) < strlen(s)) {
...
Thanks for your answers nonetheless, particularly #sleske, who made me analyse my code more deeply (which led to finding the answer).
There must be an error somewhere else. I wrote a test program:
#include <stdio.h>
#include <string.h>
void main(){
const char * s = "1.8*1d8+2.5";
size_t l = strcspn(s,"*x");
printf("l: %d\n", l);
}
and compiled it with gcc on Linux. On my system, it outputs "3".
Please post a complete, working example that exhibits the problem. Then we'll see...
Your code works just as you posted it for me1.
#include <stdio.h>
#include <string.h>
int main(void) {
const char * s = "1.8*1d8+2.5";
size_t l = strcspn(s,"*x");
printf("%zd (%.*s)\n", l, (int)l, s);
return 0;
}
so ross$ ./a.out
3 (1.8)
1. Mac OS X 10.6.4
I'd use sscanf instead of manually finding stop points and parsing myself. You can get your integers and floats easily out of the string with that.
This code:
#include <stdio.h>
#include <string.h>
int main(void)
{
const char * s = "1.8*1d8+2.5";
size_t l = strcspn(s,"*x");
printf("<<%s>> %zd <<%s>>\n", s, l, s+l);
return 0;
}
produces this answer on MacOS 10.6.4 (GCC 4.5.1):
<<1.8*1d8+2.5>> 3 <<*1d8+2.5>>
If your compiler and library does not produce the same answer, get a fixed/upgraded version of the software.
(NB: The 'z' modifier in the printf() format string is a C99 feature - it indicates that the type of the parameter is size_t.)
I need to compute imaginary exponential in C.
As far as I know, there is no complex number library in C. It is possible to get e^x with exp(x) of math.h, but how can I compute the value of e^(-i), where i = sqrt(-1)?
In C99, there is a complex type. Include complex.h; you may need to link with -lm on gcc. Note that Microsoft Visual C does not support complex; if you need to use this compiler, maybe you can sprinkle in some C++ and use the complex template.
I is defined as the imaginary unit, and cexp does exponentiation. Full code example:
#include <complex.h>
#include <stdio.h>
int main() {
complex x = cexp(-I);
printf("%lf + %lfi\n", creal(x), cimag(x));
return 0;
}
See man 7 complex for more information.
Note that exponent of complex number equals:
e^(ix) = cos(x)+i*sin(x)
Then:
e^(-i) = cos(-1)+i*sin(-1)
Using the Euler's Formula you have that e^-i == cos(1) - i*sin(1)
e^-j is just cos(1) - j*sin(1), so you can just generate the real and imaginary parts using real functions.
Just use the cartesian form
if z = m*e^j*(arg);
re(z) = m * cos(arg);
im(z) = m * sin(arg);
Is calling a c++ function a solution for you? The C++ STL has a nice complex-class and boost also has to offer some nice options. Write a function in C++ and declare it "extern C"
extern "C" void myexp(float*, float*);
#include <complex>
using std::complex;
void myexp (float *real, float *img )
{
complex<float> param(*real, *img);
complex<float> result = exp (param);
*real = result.real();
*img = result.imag();
}
Then you can call the function from whatever C-code you rely on ( Ansi-C, C99, ...).
#include <stdio.h>
void myexp(float*, float*);
int main(){
float real = 0.0;
float img = -1.0;
myexp(&real, &img);
printf ("e^-i = %f + i* %f\n", real, img);
return 0;
}
In C++ it can be done directly:
std::exp(std::complex<double>(0, -1));