Implementing the scott T method - c

I've trying to implement a math formal to convert a 3 phase system into 2 phases, using the following code:
#include <stdio.h>
#include <math.h>
#define PI 3.14159265358979
void getAngle( float xValue , float yValue, float zValue){
float x = 0.0;
float y = 0.0;
float z = 0.0;
double rad_angle =0.0;
double angle =0 ;
double zaehler = 0.0;
double nenner = 0.0;
z=xValue;
y=yValue;
x=zValue;
printf(" Getangle \n" );
printf(" X = %lf , [BY = %lf , Z = %lf \n " , x,y,z);
zaehler =z-x;
nenner = ((y-x) -((x-z)/2-(z-y)/2))*(2*sqrt(3));
printf (" the sin value is : %lf \n" , zaehler);
printf (" the cos value is : %lf \n",nenner);
rad_angle = atan2(nenner,zaehler);
printf("Radangle = %lf \n",rad_angle);
angle =( rad_angle * 180/PI);
printf("Angle in degree = %lf \n",angle);
}
void setAngle(float angle ){
float x = 0 ;
float y = 0 ;
float z = 0 ;
printf(" the given angle is : %lf \n",angle);
angle = angle *PI/180;
x = sin(angle);
y = sin(angle+(120* (PI/180.0)));
z = sin(angle+(240* (PI/180.0)));
printf(" Set angle \n" );
printf(" the x value : %f ,\n the y value : %f ,\n the z value :%f ",x,y,z);
getAngle(x,y,z);
}
int main (){
float angle = 0;
while (1){
printf(" angle in Degrees \n");
scanf("%f" ,&angle);
printf(" sinus angle : %f", sin(angle*PI/180));
setAngle(angle);
}
return 0;
}
and here is what I'm trying to implement:
I never get the angle that I give back.
Any idea what I'm doing wrong here ?

nenner = (y -((x+z)/2))*(2/sqrt(3));

here it is fixed up, I had to chage the order of x,y,z and fix the nenner formula.
void getAngle( float xValue , float yValue, float zValue){
float x = 0.0;
float y = 0.0;
float z = 0.0;
float rad_angle =0.0;
float angle =0 ;
float zaehler = 0.0;
float nenner = 0.0;
y=xValue;
z=yValue;
x=zValue;
printf(" Getangle \n" );
printf(" X = %lf , [BY = %lf , Z = %lf \n " , x,y,z);
zaehler =z-x;
nenner = (y -((x+z)/2))*(2/sqrt(3));
printf (" the sin value is : %lf \n" , zaehler);
printf (" the cos value is : %lf \n",nenner);
rad_angle = atan2(nenner,zaehler);
printf("Radangle = %lf \n",rad_angle);
angle =( rad_angle * 180/PI);
printf("Angle in degree = %lf \n",angle);
}

Related

Specifying float or double yet getting int value

#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.

How to print the imaginary root of quadratic equation in c?

I need to print the imaginary root in the quadratic equation.
but when I execute my code the result shows me that the imaginary root is 0.00000i.
even I use to <complex.h> also same.
Can everybody help me to check the code that I bold?
//C program to find the root of the quadratic equation
#include<stdio.h>
#include<math.h>
#include<complex.h>
int main()
{
double a, b, c, x, x1, x2, disc, xr, ximg1, ximg2;
printf("Please enter the value of quadratic equation, a: ");
scanf("%lf", &a);
printf("Please enter the value of quadratic equation, b: ");
scanf("%lf", &b);
printf("Please enter the value of quadratic equation, c: ");
scanf("%lf", &c);
if( a == 0 )
{
x = -(c/b);
printf("\nThis is not a quadratic equation.\n");
printf("x = %.3lf", x);
}
else{
disc = (b*b) - 4*a*c;
if( disc == 0 ){
x1 = -b / 2 * a;
x2 = -b / 2 * a;
printf("x1 = %lf, x2 = %lf", x1, x2);
}
else if(disc > 0){
x1 = ( -b + sqrt( disc ) ) / 2 * a;
x2 = ( -b - sqrt( disc ) ) / 2 * a;
printf("x1 = %.1lf, x2 = %.1lf", x1, x2);
}
else{
ximg1 = sqrt( disc ) / 2 * a;
ximg2 = - sqrt( disc ) / 2 * a;
xr = - b / ( 2 * a );
**printf("xr = %lf, ximg1 = %lfi, ximg2 = %lfi", crealf(xr), cimagf(ximg1), cimagf(ximg2));**
}
}
return 0;
}
The output are shown as below:
Please enter the value of quadratic equation, a: 4
Please enter the value of quadratic equation, b: 1
Please enter the value of quadratic equation, c: 3
xr = -0.125000, ximg1 = 0.000000i, ximg2 = 0.000000i
Process returned 0 (0x0) execution time : 3.914 s
Press any key to continue.
You should print the roots as complex numbers using the computed real and imaginary parts. No need for <complex.h> nor complex types:
double xr = - b / ( 2 * a );
double ximg1 = -sqrt( -disc ) / (2 * a);
double ximg2 = -ximg1;
printf("x1 = %lf%+lfi, x2 = %lf%+lfi\n", xr, ximg1, xr, ximg2);
As you have used complex header file you just need to use the csqrt() instead of sqrt(),
//C program to find the root of the quadratic equation
#include<stdio.h>
#include<math.h>
#include<complex.h>
int main()
{
double a, b, c, x, x1, x2, disc, xr, ximg1, ximg2;
printf("Please enter the value of quadratic equation, a: ");
scanf("%lf", &a);
printf("Please enter the value of quadratic equation, b: ");
scanf("%lf", &b);
printf("Please enter the value of quadratic equation, c: ");
scanf("%lf", &c);
if( a == 0 )
{
x = -(c/b);
printf("\nThis is not a quadratic equation.\n");
printf("x = %.3lf", x);
}
else{
disc = (b*b) - 4*a*c;
if( disc == 0 ){
x1 = -b / 2 * a;
x2 = -b / 2 * a;
printf("x1 = %lf, x2 = %lf", x1, x2);
}
else if(disc > 0){
x1 = ( -b + csqrt( disc ) ) / 2 * a;//changed the function here
x2 = ( -b - csqrt( disc ) ) / 2 * a;//changed the function here
printf("x1 = %.1lf, x2 = %.1lf", x1, x2);
}
else{
ximg1 = sqrt( disc ) / 2 * a;
ximg2 = - sqrt( disc ) / 2 * a;
xr = - b / ( 2 * a );
**printf("xr = %lf, ximg1 = %lfi, ximg2 = %lfi", crealf(xr), cimagf(ximg1), cimagf(ximg2));**
}
}
return 0;
}
Complex types not needed
When code reaches the below, disc < 0. Find the square root of the negation.
// ximg1 = sqrt( disc ) / 2 * a;
ximg1 = sqrt( -disc ) / 2 * a; // Use -disc
// ximg2 = - sqrt( disc ) / 2 * a;
ximg2 = -ximg1; // Simply negate ximg1
xr = - b / ( 2 * a );
printf("xr = %lf, ximg1 = %lfi, ximg2 = %lfi",
// crealf(xr), cimagf(ximg1), cimagf(ximg2));
xr, ximg1, ximg2);
Tip: use "%e", it is more informative.
printf("xr = %le, ximg1 = %lei, ximg2 = %lei",
xr, ximg1, ximg2);
Bug
Instead of / 2 * a;, in various places, certainly you want / (2 * a);.

How to get second output as NO

I am writing code to find if a person breaks world record in running race.
The first input is number of test cases( t ), then the next inputs are conditions faced while running ( k1, k2, k3, v)
If final speed is equal to or greater than 9.58 the output should be no.
While if final speed is less than 9.58 the output should be yes.
But in following test case I am getting wrong output for second case:
3
1.0 1.0 1.0 10.45
1.0 1.0 1.0 10.44
1.0 1.0 0.9 10.44
I get Output :
YES
YES
NO
I want output :
YES
NO
NO
int main(void)
{
int t;
scanf("%d", &t);
float k1, k2, k3, v, speed_with_fact[t], final_speed[t];
for (int i = 0; i < t; i++)
{
scanf("%f %f %f %f", &k1, &k2, &k3, &v);
speed_with_fact[i] = 100 / (k1 * k2 * k3 * v);
speed_with_fact[i] = (int)(speed_with_fact[i] * 100 + 0.5);
// printf("%f\n " ,speed_with_fact[i]);
final_speed[i] = speed_with_fact[i] / 100;
// printf("%f\n " ,final_speed[i]);
}
for (int j = 0; j < t; j++)
{
//printf("%f\n " ,final_speed[j]);
if (final_speed[j] < 9.58)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
return 0;
}
The value of final_speed[j] being 9.58 is an aproximation, if you test print it with printf("%.10f\n " ,final_speed[j]); you'll see that the value is not exactly 9.58 (I got 9.5799999237). Floating-point arithmetic is very inexact and is the source of many problems like your own.
In your particular case, since the type of the literal 9.58 will be double by default it means that you are comparing float with double, this is fixed if you use double variables.
You could force a float literal using 9.58f as very accurately pointed out by MOhem and it would also work, but using double is advised.
int main(void)
{
int t;
scanf("%d", &t);
double k1, k2, k3, v, speed_with_fact[t], final_speed[t]; //<-- here
for (int i = 0; i < t; i++)
{
scanf("%lf %lf %lf %lf", &k1, &k2, &k3, &v); //<-- here
speed_with_fact[i] = 100 / (k1 * k2 * k3 * v);
//...
}
Another good suggestion by MOehm's will also work and is a good option, maybe even better as it simplifies the comparison to int vs int.

Programming Sine and Cosine in C more efficently

Im writing a C code programm that calcultates sine and cosine of a given angle without using the Sine and Cosine Functions of the Math.h library.
But the problem I am facing right now is that i can only calculate the sine and cosine of the Angles between -90° - 90° (so the angles in the first and fourth quadrant). The Cosine(100) = Cosine(80) with a negative operator. So my way of thinking would be to just write code that whenever it gets an angle that is greater than 90 and smaller than 270, it should just substract the additional value from 90; so in the case of Cos(240) that would be the same as Cos(90-150) with an inverted operator infront.
How should one go about this, without having to write 180-if statements?
#include <stdio.h>
#include <math.h>
int main() {
double alpha[29];
alpha[0] = 45.00000000;
alpha[1] = 26.56505118;
alpha[2] = 14.03624347;
alpha[3] = 7.12501635;
alpha[4] = 3.57633437;
alpha[5] = 1.78991061;
alpha[6] = 0.89517371;
alpha[7] = 0.44761417;
alpha[8] = 0.22381050;
alpha[9] = 0.11190568;
alpha[10] = 0.05595289;
alpha[11] = 0.02797645;
alpha[12] = 0.01398823;
alpha[13] = 0.00699411;
alpha[14] = 0.00349706;
alpha[15] = 0.00174853;
alpha[16] = 0.00087426;
alpha[17] = 0.00043713;
alpha[18] = 0.00021857;
alpha[19] = 0.00010928;
alpha[20] = 0.00005464;
alpha[21] = 0.00002732;
alpha[22] = 0.00001366;
alpha[23] = 0.00000683;
alpha[24] = 0.00000342;
alpha[25] = 0.00000171;
alpha[26] = 0.00000085;
alpha[27] = 0.00000043;
alpha[28] = 0.00000021;
double x = 0.60725294;
double y = 0;
double winkel = -150;
double theta = winkel;
double xs;
double ys;
int i = 0;
}
while ( i < 29 ){
printf("This is run number %d with theta = %lf \n", i, theta);
xs = y / pow(2, i);
ys = x / pow(2, i);
if (theta <= 0){
x = x + xs;
y = y - ys;
theta = theta + alpha[i];
} else {
x = x - xs;
y = y + ys;
theta = theta - alpha[i];
};
printf("x = %lf and y = %lf \n \n",x,y);
i++;
}
printf("cosine = %lf\n", x);
printf("sine = %lf\n", y);
return 0;
}
cos(x) = cos(-x)
cos(x) = cos(x%360) if x is in degrees and x is positive
those identities should be sufficient to understand what to do, right?
likewise sin(-x) = -sin(x)
sin(x) = sin(x%360) if x is in degrees and x is positive

Why am I getting an output of 0

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.

Resources