Why am I getting an output of 0? I think there's something wrong about my angle conversion and possibly my equation, yet fiddling around with it and moving some stuff always gives me the same result.
My goal is to write a C code that will compute the angle θ for any two given vectors u and v.
#include <stdio.h>
#include <math.h>
int main()
{
double ux, uy;
double vx, vy;
double inner_product(double vx, double vy, double ux, double uy);
double v;
double u;
double i;
double x;
double k;
double pi;
double angle;
double p;
ux = 1.0;
uy = 1.0;
vx = 1.0;
vy = 1.0;
printf ("input value for ux\n", ux);
scanf_s ("%f", &ux);
printf ("input value for uy\n", uy);
scanf_s ("%f", &uy);
printf ("input value for vx\n", vx);
scanf_s ("%f", &vx);
printf("input value for vy\n", vy);
scanf_s ("%f", &vy);
u = ux * vx;
v = uy * vy;
i = u * v;
x = u * u;
k = v * v;
pi = acos(-1.0);
p = acos(i / (sqrt(x * k)));
angle = ((p * 180) / pi); //converting from radians to degrees
printf("%f", angle);
return;
}
The math error is in the following:
p = acos(i / (sqrt(x * k)));
Change it to:
p = acos((ux*vx + uy*vy) / (sqrt(ux*ux + uy*uy) * sqrt(vx*vx + vy*vy)));
That's just the dot product divided by the two lengths.
You equation is wrong.
The correct is:
|U|=√[Ux^2+Uy^2]
|V|=√[Vx^2+Vy^2]
U*V=(Ux,Uy)(Vx,Vy)=Ux*Uy+Vx*Vy
cos=U*V/[|U|*|V|]
Not %f but %lf specifier have to be used to read data having type double via scanf() family.
Note that %f should be used to print data having type double via printf() family because float will be automatically converted to double for variable number arguments.
Related
#include <stdio.h>
int diameter_fn(int r)
{
return (2 * r);
}
void circumference_fn(int r)
{
float pie = 22 / 7;
float circum = (2 * pie * r);
printf(", Circumference = %f", circum);
}
void area_fn(int r)
{
float pie = 22 / 7;
float area = (22 * r * r / 7);
printf(" & the Area = %f", area);
}
int main()
{
printf("\nName = Parth_Agrawal & UID = 22BCS10924\n");
int radius;
printf("Enter the Radius of Circle:\t\t");
scanf("%d", &radius);
printf("\nDiameter = %d", diameter_fn(radius));
circumference_fn(radius);
area_fn(radius);
return 0;
}
I want to calculate Circumference, diameter and area of circle using functions yet I get non-perfect Circumference and area values.
I already tried replacing the float with double, %f with %lf etc but I am always getting the Circumference and area in xxx.0000 format,I.e, similar to Int converted to float format.
Like the area for 4 unit radius is 50.27 but it is giving me 50.000000 which is too much annoying.
This is the Result I am getting
whereas this is the Result which I should get
... but it is giving me 50.000000 ...
OP is using integer math in many places where floating point math is needed.
void circumference_fn(int r) {
float pie = 22 / 7; // Integer math!!
float circum = (2 * pie * r);
printf(", Circumference = %f", circum);
}
void area_fn(int r) {
float pie = 22 / 7; // Integer math!! pie not used
float area = (22 * r * r / 7);// Integer math!!
printf(" & the Area = %f", area);
}
Instead use FP math.
Scant reason to use float. Use double as the default FP type.
Rather than approximate π with 22/7, use a more precise value.
#define PIE 3.1415926535897932384626433832795
void circumference_fn(int r) {
double circum = (2 * PIE * r);
printf(", Circumference = %f", circum);
}
void area_fn(int r) {
double area = PIE * r * r;
printf(" & the Area = %f", area);
}
Other
All three functions should take a double argument and return a double.
Use "%g" for printing. It is more informative with wee values and less verbose with large ones.
#Shawn is right: you are using integer math to calculate Pi. You should #include <math.h> and use M_PI instead of trying to calculate it yourself.
I'm having an assignment to complete where I need to find the zero body bias threshold voltage,based on the value of Vsb. Though this problem is based on the solution of another exercise, I give to you below some screenshots of what I need to calculate:
Where the Vthreshold formula is:
So I want to implement this:
My problem is that I don't know how to insert the Vsb value to the final formula in a way so that it can affect the final value of the zero body bias.The code that I've written returns only the value of the zero body bias based on the vthreshold only
Here's the code that I've written so far:
#include <stdio.h>
#include <math.h>
double vFB (double fgs, double B);
double vT (double A, double C);
double vA (double D);
double vSP (double J);
double gamwt (double A, double F);
double roulis( double H, double L);
double thres_volt(double vFB, double vSP, double vT, double vA);
double zero_body_bias_threshold_voltage(double thresh_volt, double gamma, double roulis);
int main(){
double eps0 = 8.854E-14; //Dielectric constant, void, [F/cm]
double epsox = 3.9*eps0; //Oxide dielectric constant, [F/cm]
double epsSi = 11.2*eps0; //Si dielectric constant, [F/cm]
double q = 1.6E-19; // expressed in Coulomb
double ni = 1.45E10; // intrincic carrier concentration expressed in cm^-3
double Na = 1.0E15; // expressed in cm^-3
double Ndpoly = 1.0E19; // expressed in cm^-3
double xox = 100.0; //Oxide thickness,[A]
double DI = 2.0E12; // number of implanted ions/cm^2 expressed in cm^-2
double kTq = 0.026; // expressed in Volt
double Qf = q*1.0E11; //expressed in C/cm2
double Vsb;
double Cox = (epsox)/(xox*1.0E-8); //Oxide capacitance per unit area, [F/cm2]
double A = 1.0/Cox;
double G = ((Na*Ndpoly))/((ni*ni));
double fgs = -((kTq)*log(G));
double B = A*Qf;
double T = Na/ni;
double J = 2*kTq*log(T);
double K = 2*q*epsSi*Na*J;
double C = sqrt(K);
double W = (q*DI)*(1.0/(Cox));
double D = W;
double U = 2*q*epsSi*Na;
double F = sqrt(U);
double ginomeno1 = J + Vsb;
double tetragwniki_tou_ginomenou1 = sqrt(ginomeno1);
double H = tetragwniki_tou_ginomenou1;
double ginomeno2 = J;
double tetragwniki_tou_ginomenou2 = sqrt(ginomeno2);
double L = tetragwniki_tou_ginomenou2;
double v1 = vFB(fgs, B);
double v2 = vSP(J);
double v3 = vT(A,C);
double v4 = vA(W);
double v5 = gamwt(A,F);
double v6 = roulis(H,L);
double v7 = thres_volt(v1,v2,v3,v4);
double v8 = zero_body_bias_threshold_voltage (v7,v5,v6);
double FocmtofFoum = 1.0E11; //Conversion factor of F/cm to fF/um
double Coxnew = Cox*FocmtofFoum;
printf ("Cox = %e [F/cm2]\n",Cox);
printf ("Coxnew = %f [fF/um]\n",Coxnew);
printf ("VFB = %f [V]\n", v1);
printf ("Vsp = %f [V]\n", v2);
printf ("Vt =%f [V]\n", v3);
printf ("Va = %f [V]\n", v4);
printf ("gamma = %f [V ^1/2]\n", v5);
printf ("Give me a number for Vsb..");
scanf ("%f", &Vsb);
if (Vsb==0)
ginomeno1 = J;
printf ("V_body_bias = %f [V]\n", v8);
getch();
return 0;
}
double vFB (double fgs, double B)
{
return (fgs-B);
}
double vT (double A, double C){
return (A*C);
}
double vA (double D){
return (+D);
}
double vSP (double J)
{
return (+J);
}
double gamwt (double A, double F)
{
return (A*F);
}
double roulis( double H, double L)
{
return (H-L);
}
double thres_volt(double vFB, double vSP, double vT, double vA)
{
return (vFB+vSP+vT+vA);
}
double zero_body_bias_threshold_voltage(double thresh_volt, double gamwt, double roulis)
{
return (thresh_volt + gamwt*roulis);
}
I am trying to calculate Maxwell-Boltzmann Distribution but this code gives 0.00000, what is the problem?
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
float e=2.718228183, pi=3.14159265, m=2.66*pow(10,-23), t, k=1.38*pow(10,-23), v, result;
scanf("%f %f", &t, &v);
result = sqrt(pow( m / (2*pi*k*t), 3)) * 4 * pi * pow(v,2) * pow(e, -(m * pow(v,2)) / (2*k*t));
printf("%f", result);
}
As described in the comments, the use of float together with the reduced precision of the constants give a result that is not representable anymore as a float. Changing the data type to double alone gives two decimal digits of accuracy. If we use exp, more digits for pi and do a bit of recombination of the computations we get 12 digits of accuracy. E.g.:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
double pi = 3.1415926535897932384626433832795028842, m = 2.66e-23, k =
1.38e-23;
double t, v, v2, dkt, result;
// check omitted
scanf("%lf %lf", &t, &v);
v2 = v * v;
dkt = 2 * k * t;
result = pow(m / (pi * dkt), 3 / 2.0) * 4 * pi * v2 * exp(-(m * v2) / (dkt));
printf("%.20g\n", result);
return 0;
}
The result from Pari/GP is 8.1246636077915008261803395870165527173e-9 and the result we get with the code above is 8.1246636077914841125e-09. Without the intermediate results v2, dkt and the replacement of sqrt we got 8.1246636077914824582e-09, not much of a difference, especially with accuracy where it gained nothing.
If you want the full 16 decimal digits of accuracy you need to take the whole thing apart and take a different approach.
replace
double pi=acos(-1.);
instead of
double pi=3.1415926535897932384626433832795028842;
I am making a calculator using the formula on this link:
http://cereference.com/book/surveying-and-transportation-engineering/simple-curves-or-circular-curves#sthash.qrD1VOm6.08csgYq9.dpbs
and
https://www.easycalculation.com/engineering/civil/highways-horizontal-curve.php
EDITED QUESTION!
So I used the math.h library in order to use the sin, tan, cos, and sec function but the answers are not right based on my formula... So to test, lets say I have an angle of 36 and a radius of 286... so the answer for the tangent (utangent) must be 92.927. and my next question is that how to use the sec function? I commented it because it wont compile... Also with tan,sin and cos.
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main(){
double length, angle, radius, tangent, chord, midordinate, external, degree;
double pcurve, pintersection, ptangent;
double ulength, uangle, uradius, utangent, uchord, umidordinate, uexternal;
double pi;
double choice, choice2, given;
pi = 3.14159;
printf("Enter radius: ");
scanf("%lf",&radius);
printf("Enter angle: ");
scanf("%lf",&angle);
utangent = radius * (tan(angle/2));
uchord = 2*radius*(sin(angle/2));
umidordinate = radius - (radius*(cos(angle/2)));
//uexternal = radius * (sec(angle/2)) - radius;
printf("tangent = %lf\n",utangent);
printf("chord = %lf\n",uchord);
printf("ordinate = %lf\n",umidordinate);
//printf("%lf\n",uexternal);
getch();
return 0;
}
If you compile your code with warnings, which you absolutely should, you may see something like:
sintancos.c:15:13: warning: format specifies type 'float *' but the argument has type 'double *' [-Wformat]
scanf("%f", &angle);
~~ ^~~~~~
%lf
This is fixed by rewriting it to scanf("%lf", &angle); as suggested by the warning message.
I assume you need to recalculate the input from degrees to radians, since you're asking for degrees. And, of course, before outputting it again you need to change it back to degrees.
It's often done in C with macros, but I prefer functions.
double to_degrees(double rad)
{
return rad * 180.0 / M_PI;
}
double to_radians(double deg)
{
return deg * M_PI / 180.0;
}
M_PI is almost always defined in math.h, with higher precision than your pi. You should also move your input and calculation to its own functions, so it is easier to read and test.
sec is not a standard C function, so you have to define it yourself. It'll be something like this:
#include<stdio.h>
#include<math.h>
double to_degrees(double rad)
{
return rad * 180.0 / M_PI;
}
double to_radians(double deg)
{
return deg * M_PI / 180.0;
}
double sec(double z_r)
{
return 1 / cos(z_r);
}
int main(){
double angle, radius, angle_r;
double utangent, uchord, umidordinate, uexternal;
//printf("Enter radius: ");
//scanf("%lf",&radius);
radius = 286;
//printf("Enter angle: ");
//scanf("%lf",&angle);
angle = 36;
angle_r = to_radians(angle);
utangent = radius * (tan(angle_r/2));
uchord = 2*radius*(sin(angle_r/2));
umidordinate = radius - (radius*(cos(angle_r/2)));
uexternal = radius * (sec(angle_r/2)) - radius;
printf("\nResults:\n");
printf("tangent = %lf\n",utangent);
printf("chord = %lf\n",uchord);
printf("ordinate = %lf\n",umidordinate);
printf("external %lf\n",uexternal);
return 0;
}
I need to get the user to input a number into a program and then need to be able to use that number in many other parts functions in the program. Any way to do that?
Here is the code:
#include <stdio.h>
#include <math.h>
#define W 8.
#define H 4.
double ellipse(double);
typedef double (*DfD) (double);
double simpsons_int (DfD, double, double, int);
int main()
{
double len, w, h, volume;
printf("Please enter a length, width and height (in meters) of the an elliptical storage tank \n");
scanf("%lf %lf %lf", &len, &w, &h);
double a = h/2.*-1., r;
for (double depth=10; depth<=400; depth=depth+10)
{
r=a+(depth/100);
volume = len*simpsons_int(ellipse, a, r, 10000);
printf("depth is %.1f, volume is %f\n", depth, volume);
}
}
double ellipse(double y)
{
double x;
double A=W/2.;
double B=H/2.;
x=2*sqrt((1-(y*y)/(B*B))*(A*A));
return x;
}
double simpsons_int(DfD f, double y0, double y1, int n)
{
double y, sum, dy = (y1 - y0)/n;
sum = f(y1) + f(y0);
for(y = y0; y <= y1-dy; y += dy)
sum += 2.0 * f(y+dy) + 4.0 * f(y + dy/2);
return sum * dy / 6.0;
}
but I need H and W to be number that are input by the user not 8 and 4.
You can either pass it as argument of the function, or declare it as global variable. I'd rather use the first, depending on the application.
1) passing as parameter. Your function should be:
double ellipse(double y, double W, double H )
{
double x;
double A=W/2.;
double B=H/2.;
x=2*sqrt((1-(y*y)/(B*B))*(A*A));
return x;
}
And then you declare and scanf W and H within main()
2) Just declare W and H before main();
double W,H;
int main()
{
double len, w, h, volume;
printf("Please enter a length, width and height (in meters) of the an elliptical storage tank \n");
scanf("%lf %lf %lf", &len, &w, &h);
scanf("%lf %lf",&W,&H);
double a = h/2.*-1., r;
for (double depth=10; depth<=400; depth=depth+10)
{
r=a+(depth/100);
volume = len*simpsons_int(ellipse, a, r, 10000);
printf("depth is %.1f, volume is %f\n", depth, volume);
}
}
Preprocessor directives like #define must be known at compile-time. Think of them as constants: you can set them, but as soon as you run the program they're set in stone.
You should be using your variables to do this; you could possibly define w and h to be global variables, but better practice would be to pass them in as parameters to the ellipse function.