I'm compiling this program using Code::Blocks 10.05 however normally I will get about 10 iterations done before it starts producing Nan in every single output. I was wondering if this is a problem caused by using the cos and sin functions and if there was a decent work around to avoid this?
I have to produce a lot of iterates because I am working on a project for University so it has to be accurate too. I looked up a few articles about how to avoid using sin and cos though I need to follow a few formulas rigorously otherwise the results I produce may be inaccurate so I'm not sure whether to compromise.
struct Particle // Need to define what qualities our particle has
{
double dPosition;
double dAngle;
};
Particle Subject;
void M1(double &x, double &y) //Defines movement if particle doesn't touch inner boundary
{
x = x + 2*y;
}
double d = 0.25; //This can and will be changed when I need to find a distance between
// the two cricles at a later stage
void M2(double &x,double &y, double d) //Defines movement of a particle if it impacts the inner boundary
{
double z = asin(-(sin(y)+d*cos(x + y))/0.35);
double y1 = y;
y = asin(-0.35*sin(z) + d*cos(x + y + 2*z));
x = y + y1 + x + 2*z;
}
int main()
{
cout << "Please tell me where you want this particle to start positions-wise? (Between 0 and 2PI" << endl;
cin >> Subject.dPosition;
cout << "Please tell me the angle that you would like it to make with the normal? (Between 0 and PI/2)" << endl;
cin >> Subject.dAngle;
cout << "How far would you like the distances of the two middle circles to be?" << endl;
double d;
cin >> d;
// These two functions are to understand where the experiment begins from.
// I may add a function to change where the circle starts however I will use radius = 0.35 throughout
cout << "So position is: " << Subject.dPosition << endl;
cout << "And angle with the normal is: " << Subject.dAngle <<endl;
int n=0;
while (n <= 100) //This is used to iterate the process and create an array of Particle data points
{ // in order to use this data to build up Poincare diagrams.
{
while (Subject.dPosition > 2*M_PI)
Subject.dPosition = Subject.dPosition - 2*M_PI;
}
{
if (0.35 >= abs(0.35*cos(Subject.dPosition + Subject.dAngle)+sin(Subject.dAngle))) //This is the condition of hitting the inner boundary
M2(Subject.dPosition, Subject.dAngle, d); //Inner boundary collision
else
M1(Subject.dPosition, Subject.dAngle); // Outer boundary collision
};
cout << "So position is: " << Subject.dPosition << endl;
cout << "And angle with the normal is: " << Subject.dAngle <<endl;
n++;
}
return 0;
}
Nan is shown in c++ as an indication of infinite, zero devision, and some other variations of non representable numbers.
Edit:
As pointed by Matteo Itallia, inf is used for infinite/zero division. I found these approaches:
template<typename T>
inline bool isnan(T value) {
return value != value;
}
// requires #include <limits>
template<typename T>
inline bool isinf(T value) {
return std::numeric_limits<T>::has_infinity &&
value == std::numeric_limits<T>::infinity();
}
Reference: http://bytes.com/topic/c/answers/588254-how-check-double-inf-nan
If the value is outside of [-1,+1] and passed to asin(), the result will be nan
If you need to check for Nan, try the following
if( value != value ){
printf("value is nan\n");
}
Related
I am a complete beginner in programming and I was given the following assignment:
Write a C++ program that computes a pair of estimates of π, using a sequence of inscribed and circumscribed regular polygons. Halt after no more than 30 steps, or when the difference between the perimeters of the circumscribed and inscribed polygons is less than a tolerance of ε=10⁻¹⁵. Your output should have three columns, for the number of sides, the perimeter of an inscribed polygon, and perimeter of the circumscribed polygon. For the last two columns, display 14 digits after the decimal point.
well, I decided to use the law of cos to find the lengths of the sides of the polygon but when I was testing out my program I realized the line:
a = cos(360 / ngon);
keeps giving me a zero as the output which makes everything else also zero and I am not sure what is wrong please help.
P.S. Sorry if the program looks really sloppy, I am really bad at this.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#define _USE_MATH_DEFINES
#include <math.h>
#include <cmath>
using namespace std;
int main()
{
char zzz;
int ngon = 3, a, ak;
double insngon = 0.0;
double cirngon = 0.0;
cout << "Number of Sides" << "\t\t\t" << "Perimeter of insribed region" << "\t\t\t" << "Perimeneter of circumscribed polygon" << "\t\t" << "\n";
while (ngon <= 30)
{
a = cos(360 / ngon);
ak = pow(.5, 2) + pow(.5, 2) - 2 * .5*.5*a;
insngon = (ak*ngon);
cirngon = (ak / (sqrt(1 - pow(ak, 2))));
cout << fixed << setprecision(14) << ngon << " " << insngon << " " << cirngon << endl;
ngon++;
if (cirngon - insngon <= pow(10.0, -15));
cin >> zzz;
return 0;
}
cout << "\nEnter any character and space to end ";
cin >> zzz;
return 0;
}
One issue is that you declared integers, yet you are using them in the call to cos here:
int ngon = 3, a, ak;
//...
a = cos(360 / ngon);
Since a is an integer, the return value of cos (which is of type double) will be truncated. Also, since ngon is an integer, the 360 / ngon will also truncate.
The fix is to make a a double, and divide 360.0 by ngon to prevent the truncation:
int ngon = 3, ak;
double a;
//...
a = cos(360.0 / ngon);
The other issue, as pointed out in the comments is that the trigonometric functions in C++ use radians as the argument, not degrees. You need to change the argument to the equivalent value in radians.
Another issue is that you're using pow to compute values that are constant. There is no need to introduce an unnecessary function call to compute constant values. Just define the constants and use them.
For example:
const double HALF_SQUARED = 0.25
const double EPSILON_VALUE = 10.0e-15;
and then use HALF_SQUARED and EPSILON_VALUE instead of the calls to pow.
Also, pow is itself a floating point function, thus can produce results that are not exact as is discussed by this question . Thus pow(ak, 2) should be replaced with simply ak * ak.
Use float a; (or double a) instead of int a.
Here the return type of a is int
And calculating
a = cos(360/ngon)
Is equivalent to a= cos(120) that is the result of cos(120) is 0.8141 and being a integer type "a" will only store the integer part it.
Therefore 'a' will be 0 and discarding floating value.
Also use double ak; instead of int ak;.
Because here pow function has been used which have return type 'double'
I am writing a program in C++ for the distance formula. The answer to x1=0 y1=0 x2=1 y2=1 should be around 1.14, however the answer printed out is 2.00. Every single variable is stored as double I don't know what is going wrong here. Here is my code, and thank you for any help!!
// main.cpp
// Chap6_42
//
// Created on 10/21/14.
//
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
double distance(double,double,double,double); //distance prototype
int main()
{
double d = 0;
double x1 = 0; //coordinate x1
double x2 = 0; //coord x2
double y1 = 0; //coord y1
double y2 = 0; //coord y2
cout << "Enter four cords (x1,y1,x2,y2) to find the distance between them " << endl;
cout << "x1 = ";
cin >> x1;
cout << "y1 = ";
cin >> y1;
cout << "x2 = ";
cin >> x2;
cout << "y2 = ";
cin >> y2;
d = distance (x2,x1, y2,y1); //calls to distance function, performs computations
cout << "The distance is " << fixed << setprecision(2) << showpoint << d << endl;
return 0;
}
double distance(double x2,double x1,double y2,double y1) //distance function header
{
return sqrt(pow(x2-x1,2.0)) + sqrt(pow(y2-y1,2.0)); //distance function computations
}
//function definition
Your calculation is wrong. You're calling sqrt twice when you should only call it once on the entire sum.
return sqrt(pow(x2-x1,2.0) + pow(y2-y1,2.0));
Your formula is wrong.
You wrote:
sqrt(pow(x2-x1,2.0)) + sqrt(pow(y2-y1,2.0));
It should be:
sqrt(pow(x2-x1,2.0) + pow(y2-y1,2.0));
Anyway, do not use pow there but multiply by hand, that's (probably) faster and more accurate.
sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) );
Also, often you can use squared distances instead of the distance for a small performance-boost.
Why do you think the answer is around 1.14? Given your example, it should return 2.0
sqrt((pow(2.0 - 1.0, 2.0))) + sqrt((pow(2.0 - 1.0, 2.0)))
sqrt((pow(1.0, 2.0))) + sqrt((pow(1.0, 2.0)))
sqrt(1.0) + sqrt(1.0)
1.0 + 1.0
2.0
Tada!
If you are calculating distance, which the function name implies, you need to adjust your formula.
return sqrt(pow(abs(x2 - x1), 2.0) + pow(abs(y2 - y1), 2.0));
You and pythagorus disagree about how to calculate the distance
Given you intend to use "pow" for some esoteric reason, your code should be
return sqrt(abs(pow(x2-x1,2.0)) + abs(pow(y2-y1,2.0)));
or
return sqrt(pow(x2-x1,int(2)) + pow(y2-y1,int(2)));
If you are going to programming you'll probably need a lot of math. you need to know "on paper" the algorithms you are going to use (even simple ones like the one by Pitagora). Also a rounding error with doubles is not going to change a result so much (unless you are using some bad conditioned algorithm, wich is not the case for Pitagora.
Whilst working on a personal project of mine, I came across a need to divide two very large arbitrary numbers (each number having roughly 100 digits).
So i wrote out the very basic code for division (i.e., answer = a/b, where a and b are imputed by the user)and quickly discovered that it only has a precision of 16 digits! It may be obvious at this point that Im not a coder!
So i searched the internet and found a code that, as far as i can tell, uses the traditional method of long division by making a string(but too be honest im not sure as im quite confused by it). But upon running the code it gives out some incorrect answers and wont work at all if a>b.
Im not even sure if there's a better way to solve this problem than the method in the code below!? Maybe there's a simpler code??
So basically i need help to write a code, in C++, to divide two very large numbers.
Any help or suggestions are greatly appreciated!
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std; //avoids having to use std:: with cout/cin
int main (int argc, char **argv)
{
string dividend, divisor, difference, a, b, s, tempstring = ""; // a and b used to store dividend and divisor.
int quotient, inta, intb, diff, tempint = 0;
char d;
quotient = 0;
cout << "Enter the dividend? "; //larger number (on top)
cin >> a;
cout << "Enter the divisor? "; //smaller number (on bottom)
cin >> b;
//making the strings the same length by adding 0's to the beggining of string.
while (a.length() < b.length()) a = '0'+a; // a has less digits than b add 0's
while (b.length() < a.length()) b = '0'+b; // b has less digits than a add 0's
inta = a[0]-'0'; // getting first digit in both strings
intb = b[0]-'0';
//if a<b print remainder out (a) and return 0
if (inta < intb)
{
cout << "Quotient: 0 " << endl << "Remainder: " << a << endl;
}
else
{
a = '0'+a;
b = '0'+b;
diff = intb;
//s = b;
// while ( s >= b )
do
{
for (int i = a.length()-1; i>=0; i--) // do subtraction until end of string
{
inta = a[i]-'0'; // converting ascii to int, used for munipulation
intb = b[i]-'0';
if (inta < intb) // borrow if needed
{
a[i-1]--; //borrow from next digit
a[i] += 10;
}
diff = a[i] - b[i];
char d = diff+'0';
s = d + s; //this + is appending two strings, not performing addition.
}
quotient++;
a = s;
// strcpy (a, s);
}
while (s >= b); // fails after dividing 3 x's
cout << "s string: " << s << endl;
cout << "a string: " << a << endl;
cout << "Quotient: " << quotient << endl;
//cout << "Remainder: " << s << endl;
}
system ("pause");
return 0;
cin.get(); // allows the user to enter variable without instantly ending the program
cin.get(); // allows the user to enter variable without instantly ending the program
}
There are much better methods than that. This subtractive method is arbitrarily slow for large dividends and small divisors. The canonical method is given as Algorithm D in Knuth, D.E., The Art of Computer Programming, volume 2, but I'm sure you will find it online. I'd be astonished if it wasn't in Wikipedia somewhere.
I'm barely into my 4th week of C++ in school and was looking to be guided in the right direction.
#include "std_lib_facilities_3.h"
class BadArea{};
int area(int length, int width){
if(length <= 0 || width <=0) throw BadArea();
return length * width;
}
double mysqrt(double x){
if(x < 0.0) error("mysqrt");
return 1.0; //dummy value for now, need to write code later
}
int main(){
try{
char length = 0;
char width = 0;
cout << "Enter length and width seperated by a space\n";
cin >> length;
cin >> width;
vector<double> v(10);
v[9] = 7.5;
cout << area(7, -10) << '\n';
cout << mysqrt(-2.0) << '\n';
return 0;
}
catch(BadArea){
cerr << "Exception: Bad area\n";
}
catch(exception& e){
cerr << "ExceptionZ: " << e.what() << '\n';
}
catch(...){
cerr << "Exception occurred\n";
}
}
And this is what the assignment is asking us;
//Check for overflow in the area function
result = length * width
if result is negative or result/length <> width, throw an exception
//Use 3 iterations of the Newton-Raphson method for mysqrt
if x is 0, result is 0 so return it
if x is 1, result is 1 so return it
otherwise,
result = (x^4 + 28x^3 + 70x^2 + 28x + 1)/(8*(1 + x)*(1 + 6x + x^2))
Change the main to have an infinite loop around the try/catch part; in the try
block ask for length and width; if cin fails then return, otherwise print
the area, and print mysqrt of the area. Name your program hw3pr2.cpp. (Recall
that cin will fail if you type something that is not a properly-formatted int,
e.g., the word "end".)
I understand how to read the code, but I'm having a hard time starting it, and sort of get confused with "scope" so far it compiles correctly but keeps on giving me Range Error: 10. does that mean I'm using the class area wrong?
could someone please point me in the right direction?
Thank you!
You're declaring a vector of 10 elements and trying to access the 11th item with v[10].
[EDIT] As others have pointed out, std::vector doesn't do bounds checking by default, but if "std_lib_facilities_3.h" is similar to this, then it defines its own range-checked vector class.
[EDIT2] So you've updated your code so that length and width must be both greater than 0 or an BadArea exception will be thrown, but you're always calling area(7, -10), so you'll always get the exception. I think you want to pass the length and width to the area function: cout << area(length, width) << '\n';
I need help on how to get nth root of some number.
User enters number n and number he wants root of. I need to solve this without cmath lib and with divide and conquer method.
Here's my code that doesn't work yet:
#include<iostream>
using namespace std;
float pow(float a,float c){
if (a == 0)
return 0;
else if(a == 1)
return 1;
else{
float p = pow(a,(c/2));
if(c%2)
return p*p*a;
else
return p*p;
}
}
int main(){
float a,b;
float c;
cout << "Enter positive number:(base)" << endl;
do{
cin >> a;
}while (a < 0);
cout << "Enter number: (root)" << endl;
cin >> b;
c = 1/b;
cout << "Result:"<<pow(a,c) << endl;
system("pause");
return 0;
}
Any ideas on how to approach this problem would be more than useful.
Let me tell you how you can use divide and conquer for finding square root. The nth root would be similar.
For a given number x, you need to search for it's square root between 0 and x. Divide it by 2 = x2. If the x2 * x2 < x then your search space moves to x2 -> x or else it will be 0 -> x2. If x2 * x2 matches x then your square root is x2. Similar technique for nth root.
For those not doing numerical experiments: use the <cmath> functions sqrt and cbrt (cube-root) to construct the any root that is factorable by 2 and 3. For example, the 4th root is sqrt(sqrt(x)) and the 6th root is sqrt(cbrt(x)). If you need something for general use you can construct a recursive function which calls sqrt and cbrt appropriately.
I'm guessing this will give a faster and more accurate answer than pow, if that matters. If it doesn't, just use pow.