Fractions instead of decimals - c++

So, I am Writing this little program in c++, it's made to compute various values with lines (sorry i am french, i don't know how to say it in English, but they are the lines with equation types Y = kx + t).
And I want my program to output fractions instead of decimals (2/3 instead of 0.666666666...).
Can anyone tell me how ?
I read online that there are some libraries for that purpose, can anyone help me on how to use them and/or how to implement them in my code ?
Thanks :)
#include "pch.h"
#include <iostream>
#include <string>
std::string mainAnswer;
bool endVar = false;
void lineEquationFromTwoPoints() {
mainAnswer.clear();
double Xa = 0;
double Ya = 0;
double Xb = 0;
double Yb = 0;
double Y = 0;
double X = 0;
double k = 0;
double t = 0;
std::cout << ("Enter the Coordinates of your first point in this format x y : ");
std::cin >> Xa >> Ya;
std::cout << ("Enter the Coordinates of your second point in this format x y : ");
std::cin >> Xb >> Yb;
if (Xb != Xa && Yb != Ya) {
k = (Yb - Ya) / (Xb - Xa);
t = -(Xa)*k + Ya;
if (k != 1 && t != 0) {
std::cout << ("Y = ") << k << ("x + ") << t << std::endl;
}
else if (k == 1) {
std::cout << ("Y = ") << ("x") << ("+") << t << std::endl;
}
else if (t == 0) {
std::cout << ("Y = ") << k << ("x") << std::endl;
}
}
else if (Xb == Xa) {
std::cout << ("Coordinates of the first point are Equal");
}
else if (Yb == Ya) {
std::cout << ("Coordinates of the second point are Equal");
}
else if (Xb == Xa && Yb == Ya) {
std::cout << ("Coordinates of both points are Equal");
}
}
void triangle() {
double Xa = 0;
double Ya = 0;
double Xb = 0;
double Yb = 0;
double Xc = 0;
double Yc = 0;
double Ym1 = 0;
double Xm1 = 0;
double km1 = 0;
double tm1 = 0;
double Ym2 = 0;
double Xm2 = 0;
double km2 = 0;
double tm2 = 0;
double Ym3 = 0;
double Xm3 = 0;
double km3 = 0;
double tm3 = 0;
std::cout << ("Work in progress. . . :-)") << std::endl;
}
void Choose() {
while (endVar != true) {
std::cout << ("Lines:") << std::endl;
std::cout << ("------") << std::endl << std::endl;
std::cout << ("Choose What Line Operations do You Want Me To Perform:") << std::endl;
std::cout << ("1.Formulas") << std::endl;
std::cout << ("2.Calculation of a Line's equation from 2 points") << std::endl;
std::cout << ("3.Calculation of all data in a triangle") << std::endl;
std::cout << ("Type Exit to Exit") << std::endl << std::endl;
std::getline(std::cin, mainAnswer);
if (mainAnswer == "exit" || mainAnswer == "Exit") {
std::exit;
endVar = true;
}
else if (mainAnswer == "1") {
std::cout << ("Formulas will be added Here once main program with main calculation functions will be finished") << std::endl;
}
else if (mainAnswer == "2") {
lineEquationFromTwoPoints();
}
else if (mainAnswer == "3") {
triangle();
}
else {
std::cout << ("Unexpected error occured. Please relaunch program.");
std::exit;
}
}
}
int main()
{
Choose();
return 0;
}

A nice way to approximate a float with a fraction is to used continued fractions. In the following code, epsis the desired precision. xis assumed to be strictly positive.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <tuple>
#include <vector>
#include <cmath>
// Continued fraction
std::pair<int, int> fract_cont (double x, double eps = 1.0e-3) {
std::vector<int> a;
std::vector<int> b;
a.push_back(1);
b.push_back(0);
int q = int(x);
a.push_back(q);
b.push_back(1);
double err = x - q;
double e = (x != q) ? 1.0 / (x - q) : 0.0;
int i = 1;
while (std::abs(err) > eps) {
i++;
q = int (e);
e = 1.0 / (e - q);
a.push_back (q * a[i-1] + a [i-2]);
b.push_back (q * b[i - 1] + b[i-2]);
err = x - double (a[i]) / b[i];
}
return std::make_pair(a[i], b[i]);
}
int main() {
int a, b;
double x = 4 * atan(1.0);
std::tie (a,b) = fract_cont(x);
std::cout <<"Pi = " << std::setprecision(9) << x << " ~= " << a << "/" << b << "\n";
return 0;
}
Detailed information on continued fractions is available on Wikipedia for example.
If you don't need a high precision or if you assume that the denominators will be small, you can use a brute force approach instead, simply incrementing the denominator b.

Related

Visual Studio C++ | Program crashes everytime I try to loop a cout from an array of [80]x[80]

Basically I've been playing a little with C++ and I am doing an exercise from this website called "Graduation".
So far I've been doing good for a beginner but here's my first problem which I have been struggling for the last hours and I can't find a solution online.
I am using 2 strings of arrays, one 80x80 for the map and one 100x20 for all the existing bunnies. I am putting all the data together in one string like SEX|COLOR|AGE|COORDINATES|NAME. So to extract each piece of the grid I always use 2 for functions to run throughout all the array and cut for example the 2nd character so I can know the color of each bunny in the array.
It worked fine when I used it to discover the sex of each bunny in the array but I am trying to do the same for the color and it isn't working. My program keeps crashing with the error screen I left in the screenshot I uploaded.
Heres the code:
Main.cpp
#include <iostream>
#include "Add.h"
#include "GetNames.h"
#include <string>
#include "PlaceOnMap.h"
#include "Colours.h"
using namespace std;
int start = 1;
int main()
{
while (start == 1)
{
Add(5);
PlaceOnMap(bunniestotal);
Colour();
start = 0;
}
system("pause");
return 0;
}
Add.h
#pragma once
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <string>
#include <windows.h>
#include "GetNames.h"
using namespace std;
int colours;
int cB, rB;
int bunnies = 0;
int bunniestotal = 0;
int sex = 0;
int h = 0;
string listB[10][100];
void Add(int x)
{
srand(time(NULL));
/***********************************************************************************************/
while (h < 1) {
for (int rB = 0; rB < 10; rB++) //DO THE SLOTS FOR THE BUNNIES
{
for (int cB = 0; cB < 100; cB++)
{
listB[rB][cB] = "-";
}
}
h = 1;
}
/***********************************************************************************************/
while (bunnies < x) //CHOOSE RANDOM SLOTS FOR BUNNIES
{
rB = rand() % 10;
cB = rand() % 100;
if (listB[rB][cB] == "-")
{
listB[rB][cB] = "B";
bunnies++;
}
}
bunniestotal = bunniestotal + bunnies;
bunnies = 0;
/***********************************************************************************************/
for (int rB = 0; rB < 10; rB++) //SET SEX AND COLOUR
{
for (int cB = 0; cB < 100; cB++)
{
if (listB[rB][cB] == "B")
{
sex = rand() % 2 + 1;
//cout << sex << endl;
if (sex == 1)
{
colours = rand() % 4 + 1;
switch (colours)
{
case 1: listB[rB][cB] = "mR";
break;
case 2: listB[rB][cB] = "mY";
break;
case 3: listB[rB][cB] = "mC";
break;
case 4: listB[rB][cB] = "mB";
break;
}
listB[rB][cB] = listB[rB][cB] + "0";
//cut = listB[rB][cB].substr(0, 1);
//cout << listB[rB][cB] << "Cut - " << cut << endl;
//listB[rB][cB] = listB[rB][cB] + GetNames("M") + " ";
//cout << listB[rB][cB] << endl;
//listB[rB][cB] = listB[rB][cB] + GetNames("M");
//cout << listB[rB][cB] << endl;
}
else if (sex == 2)
{
colours = rand() % 4 + 1;
switch (colours)
{
case 1: listB[rB][cB] = "fR";
break;
case 2: listB[rB][cB] = "fY";
break;
case 3: listB[rB][cB] = "fC";
break;
case 4: listB[rB][cB] = "fB";
break;
}
listB[rB][cB] = listB[rB][cB] + "0";
//cut = listB[rB][cB].substr(0, 1);
//cout << listB[rB][cB] << "Cut - " << cut << endl;
//cout << rB << " " << cB << endl;
//listB[rB][cB] = listB[rB][cB]+GetNames("F") + " ";
//cout << listB[rB][cB] << endl;
//listB[rB][cB] = listB[rB][cB] + GetNames("F");
//cout << listB[rB][cB] << endl;
}
}
}
}
}
PlaceOnMap.h
#pragma once
#include <iostream>
#include "Add.h"
#include <string>
#include <sstream>
#include <Windows.h>
#include "Colours.h"
using namespace std;
string map[80][80];
string cut;
ostringstream join;
ostringstream join1;
int xB, yB;
int hh = 0;
int capslock;
int y = 0;
int z = 0;
int u, o;
void PlaceOnMap(int bunniesn)
{
HANDLE color = GetStdHandle(STD_OUTPUT_HANDLE);
//cout << "PlaceOnMap " << listB[rB][cB] << endl;
//cout << rB << " " << cB << endl;
//cut = listB[rB][cB].substr(0, 1);
//cout << cut << endl;
/***********************************************************/
while (hh < 1)
{
for (int xB = 0; xB < 80; xB++) // CREATE MAP
{
for (int yB = 0; yB < 80; yB++)
{
map[xB][yB] = "-";
}
}
hh = 1;
}
/***********************************************************/
//cout << bunniesn << endl;
//cout << rB << endl;
//cout << cB << endl;
for (y = 0; y < 10; y++)
{
for (z = 0; z < 100; z++)
{
nextone:
if (listB[y][z].length() < 4) //DOESN'T LET BUNNIES THAT ARE ALREADY PLACED ENTER THIS LOOP
{
//cout << y << " " << z << endl;
cut = listB[y][z].substr(0, 1); //CUTS THE STRING IN ORDER TO KNOW IF IT IS A FEMALE OR A MALE
if (cut == "f" || cut == "m")
{
if (cut == "f")
{
capslock = 1;
}
else if (cut == "m")
{
capslock = 2;
}
generate:
xB = rand() % 80;
yB = rand() % 80;
if (map[xB][yB] == "-")
{
//cout << "Found a slot!" << endl;
//Sleep(2000);
//join << xB;
//join1 << yB;
listB[y][z] += to_string(xB);
listB[y][z] += to_string(yB);
//cout << "Size - " << listB[y][z].length() << endl;
if (listB[y][z].length() == 5)
{
listB[y][z] = listB[y][z] + " ";
listB[y][z] = listB[y][z] + " ";
}
else if (listB[y][z].length() == 6)
{
listB[y][z] = listB[y][z] + " ";
}
cout << listB[y][z] << endl;
//Sleep(6000);
if (capslock == 1)
{
map[xB][yB] = "f";
}
else if (capslock == 2)
{
map[xB][yB] = "m";
}
z++;
x++;
//cout << x << endl;
//Sleep(3000);
if (x < bunniesn)
{
goto nextone;
}
else
{
goto done;
}
}
else
{
goto generate;
}
}
}
}
}
done:
cout << "All done" << endl;
SetConsoleTextAttribute(color, 15);
}
Colours.h (Where the problem is happening!!)
#pragma once
#include <iostream>
#include "PlaceOnMap.h"
#include <string>
#include "Add.h"
using namespace std;
string wordString;
int t, r;
void Colour()
{
for (t = 0; t < 20; t++)
{
for (r = 0; r < 100; r++)
{
wordString = listB[t][r].substr(1, 2); //CUTS THE STRING IN ORDER TO KNOW ITS COLOR
//cout << wordString << " ";
if (wordString == "R")
{
cout << "GOT RED" << endl;
}
else if (wordString == "C")
{
cout << "GOT CYAN" << endl;
}
else if (wordString == "B")
{
cout << "GOT BLUE" << endl;
}
else if (wordString == "Y")
{
cout << "GOT YELLOW" << endl;
}
}
cout << endl;
}
}
If it is something really dumb I am sorry! Kinda noob but just trying to learn more and more! :) If you need any more info feel free to ask for!
Thank you.
In Colours.h, change for (t = 0; t < 20; t++) to for (t = 0; t < 10; t++). You only have 10 subarrays allocated, so you were going outside the bounds of the array.
From quickly looking through your code:
You defined:
string listB[10][100];
And you iterate the first index up to 20 in Colours.h:
for (t = 0; t < 20; t++)
Which works for:
listB[0][r];
listB[1][r];
listB[2][r];
listB[3][r];
listB[4][r];
listB[5][r];
listB[6][r];
listB[7][r];
listB[8][r];
listB[9][r];
And then you try accessing
listB[10][r]
which is the 11th index and not allocated.
If you redefined it somewhere and I missed I am sorry.

Organizing text file output into columns

Everything is working like it's supposed too, except when I output to the text file I can not figure out how to keep all the output lined up. My teacher wouldn't help me and I've literally been doing trial and error with "fixed, setprecision, left, setw(), etc" for a few hours now.
//Project #5
//Start Date: November 17th
//Due Date: November 23rd
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <cmath>
#include <iomanip>
using namespace std;
double f = 3.14159;
double upperBound = 0;
double lowerBound = 0;
double increment = 0;
double tempVal = 0;
double powVal = 0;
double expVal = 0;
double fact = 0;
double sinVal = 0;
double x = 0.0;
char userCont = 'Y';
int n = 0;
int i = 0;
//Factorial function using recursion...
double factorial(const int n) {
if (n <= 1) return 1;
fact = n * factorial(n - 1);
return fact;
}
//Power function using recursion...
double power(const double x, const int n) {
if (n <= 0)
return 1;
powVal = x * power(x, n - 1);
return powVal;
}
//my_sin function using power and factorial functions
double my_sin(const double x) {
sinVal = 0;
for (int k = 0; k < 50; k++) {
sinVal += power(-1, k) * (power(x, 2 * k + 1) / factorial(2 * k + 1));
}
return sinVal;
}
//my_exp(x) Function
double my_exp(const double x) {
expVal = 0;
for (int k = 0; k < 50; k++) {
expVal += power(x, k) / factorial(k);
}
return expVal;
}
int main() {
ofstream fout("output.text");
while (userCont == 'y' || userCont == 'Y') {
cout << "Enter lower and upper bounds: ";
cin >> upperBound >> lowerBound;
cout << "Enter Increment: ";
cin >> increment;
//Checking if upper and lower bounds are in the right order...
if (upperBound < lowerBound) {
tempVal = upperBound;
upperBound = lowerBound;
lowerBound = tempVal;
}
fout << "x sin(x) my_sin(x) e(x) " <<
"my_el(x) my_exp(x)" << endl;
//Loop to display and increase x by the incrememnt
for (x = lowerBound; x <= upperBound; x = x + increment) {
fout.precision(7);
fout << setw(8) << left << x << " ";
fout << setw(8) << my_sin(x) << setw(8) << " ";
fout << setw(8) << sin(x) << setw(8) << " ";
fout << setw(8) << exp(x) << setw(8) << " ";
fout << setw(8) << exp(x) << setw(8) << " ";
fout << my_exp(x) << endl;
}
cout << "Another (y/n)? ";
cin >> userCont;
}
return 0;
}
Here's how it's supposed to look
Some cleanup and the best I could to mimick the formatting, hopefully it is of some inspiration/assistance.
Important Note how I removed all the global variables. They're bad :(
Live On Coliru
// Project #5
// Start Date: November 17th
// Due Date: November 23rd
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
double factorial(const int n) {
if (n <= 1)
return 1;
return n * factorial(n - 1);
}
double power(const double x, const int n) {
if (n <= 0)
return 1;
return x * power(x, n - 1);
}
// my_sin function using power and factorial functions
double my_sin(const double x) {
double sinVal = 0;
for (int k = 0; k < 50; k++) {
sinVal += power(-1, k) * (power(x, 2 * k + 1) / factorial(2 * k + 1));
}
return sinVal;
}
// my_exp(x) Function
double my_exp(const double x) {
double expVal = 0;
for (int k = 0; k < 50; k++) {
expVal += power(x, k) / factorial(k);
}
return expVal;
}
int main() {
ofstream fout("output.text");
char userCont = 'Y';
while (userCont == 'y' || userCont == 'Y') {
cout << "Enter lower and upper bounds: ";
double upperBound, lowerBound;
cin >> upperBound >> lowerBound;
cout << "Enter Increment: ";
double increment = 0;
cin >> increment;
// Checking if upper and lower bounds are in the right order...
if (upperBound < lowerBound) {
std::swap(upperBound, lowerBound);
}
fout << " x sin(x) my_sin(x) e(x) my_el(x) my_exp(x)" << endl;
// Loop to display and increase x by the incrememnt
for (double x = lowerBound; x <= upperBound; x = x + increment) {
fout.precision(7);
fout << right << std::fixed << std::setprecision(7) << setw(12) << x << " ";
fout << right << std::fixed << std::setprecision(7) << setw(12) << my_sin(x) << setw(4) << " ";
fout << right << std::fixed << std::setprecision(7) << setw(12) << sin(x) << setw(4) << " ";
fout << right << std::fixed << std::setprecision(7) << setw(12) << exp(x) << setw(4) << " ";
fout << right << std::fixed << std::setprecision(7) << setw(12) << exp(x) << setw(4) << " ";
fout << my_exp(x) << endl;
}
cout << "Another (y/n)? ";
cin >> userCont;
}
}
Output:
x sin(x) my_sin(x) e(x) my_el(x) my_exp(x)
0.0000000 0.0000000 0.0000000 1.0000000 1.0000000 1.0000000
0.1000000 0.0998334 0.0998334 1.1051709 1.1051709 1.1051709
0.2000000 0.1986693 0.1986693 1.2214028 1.2214028 1.2214028
0.3000000 0.2955202 0.2955202 1.3498588 1.3498588 1.3498588
0.4000000 0.3894183 0.3894183 1.4918247 1.4918247 1.4918247
0.5000000 0.4794255 0.4794255 1.6487213 1.6487213 1.6487213
0.6000000 0.5646425 0.5646425 1.8221188 1.8221188 1.8221188
0.7000000 0.6442177 0.6442177 2.0137527 2.0137527 2.0137527
0.8000000 0.7173561 0.7173561 2.2255409 2.2255409 2.2255409
0.9000000 0.7833269 0.7833269 2.4596031 2.4596031 2.4596031
1.0000000 0.8414710 0.8414710 2.7182818 2.7182818 2.7182818
1.1000000 0.8912074 0.8912074 3.0041660 3.0041660 3.0041660
1.2000000 0.9320391 0.9320391 3.3201169 3.3201169 3.3201169
1.3000000 0.9635582 0.9635582 3.6692967 3.6692967 3.6692967
1.4000000 0.9854497 0.9854497 4.0552000 4.0552000 4.0552000
1.5000000 0.9974950 0.9974950 4.4816891 4.4816891 4.4816891
1.6000000 0.9995736 0.9995736 4.9530324 4.9530324 4.9530324
1.7000000 0.9916648 0.9916648 5.4739474 5.4739474 5.4739474
1.8000000 0.9738476 0.9738476 6.0496475 6.0496475 6.0496475
1.9000000 0.9463001 0.9463001 6.6858944 6.6858944 6.6858944
2.0000000 0.9092974 0.9092974 7.3890561 7.3890561 7.3890561
2.1000000 0.8632094 0.8632094 8.1661699 8.1661699 8.1661699
2.2000000 0.8084964 0.8084964 9.0250135 9.0250135 9.0250135
2.3000000 0.7457052 0.7457052 9.9741825 9.9741825 9.9741825
2.4000000 0.6754632 0.6754632 11.0231764 11.0231764 11.0231764
2.5000000 0.5984721 0.5984721 12.1824940 12.1824940 12.1824940
2.6000000 0.5155014 0.5155014 13.4637380 13.4637380 13.4637380
2.7000000 0.4273799 0.4273799 14.8797317 14.8797317 14.8797317
2.8000000 0.3349882 0.3349882 16.4446468 16.4446468 16.4446468
2.9000000 0.2392493 0.2392493 18.1741454 18.1741454 18.1741454
3.0000000 0.1411200 0.1411200 20.0855369 20.0855369 20.0855369
3.1000000 0.0415807 0.0415807 22.1979513 22.1979513 22.1979513
3.2000000 -0.0583741 -0.0583741 24.5325302 24.5325302 24.5325302
3.3000000 -0.1577457 -0.1577457 27.1126389 27.1126389 27.1126389
3.4000000 -0.2555411 -0.2555411 29.9641000 29.9641000 29.9641000
3.5000000 -0.3507832 -0.3507832 33.1154520 33.1154520 33.1154520
3.6000000 -0.4425204 -0.4425204 36.5982344 36.5982344 36.5982344
3.7000000 -0.5298361 -0.5298361 40.4473044 40.4473044 40.4473044
3.8000000 -0.6118579 -0.6118579 44.7011845 44.7011845 44.7011845
3.9000000 -0.6877662 -0.6877662 49.4024491 49.4024491 49.4024491
4.0000000 -0.7568025 -0.7568025 54.5981500 54.5981500 54.5981500
4.1000000 -0.8182771 -0.8182771 60.3402876 60.3402876 60.3402876
4.2000000 -0.8715758 -0.8715758 66.6863310 66.6863310 66.6863310
4.3000000 -0.9161659 -0.9161659 73.6997937 73.6997937 73.6997937
4.4000000 -0.9516021 -0.9516021 81.4508687 81.4508687 81.4508687
4.5000000 -0.9775301 -0.9775301 90.0171313 90.0171313 90.0171313
4.6000000 -0.9936910 -0.9936910 99.4843156 99.4843156 99.4843156
4.7000000 -0.9999233 -0.9999233 109.9471725 109.9471725 109.9471725
4.8000000 -0.9961646 -0.9961646 121.5104175 121.5104175 121.5104175
4.9000000 -0.9824526 -0.9824526 134.2897797 134.2897797 134.2897797
5.0000000 -0.9589243 -0.9589243 148.4131591 148.4131591 148.4131591
5.1000000 -0.9258147 -0.9258147 164.0219073 164.0219073 164.0219073
5.2000000 -0.8834547 -0.8834547 181.2722419 181.2722419 181.2722419
5.3000000 -0.8322674 -0.8322674 200.3368100 200.3368100 200.3368100
5.4000000 -0.7727645 -0.7727645 221.4064162 221.4064162 221.4064162
5.5000000 -0.7055403 -0.7055403 244.6919323 244.6919323 244.6919323
5.6000000 -0.6312666 -0.6312666 270.4264074 270.4264074 270.4264074
5.7000000 -0.5506855 -0.5506855 298.8674010 298.8674010 298.8674010
5.8000000 -0.4646022 -0.4646022 330.2995599 330.2995599 330.2995599
5.9000000 -0.3738767 -0.3738767 365.0374679 365.0374679 365.0374679
6.0000000 -0.2794155 -0.2794155 403.4287935 403.4287935 403.4287935
6.1000000 -0.1821625 -0.1821625 445.8577701 445.8577701 445.8577701
6.2000000 -0.0830894 -0.0830894 492.7490411 492.7490411 492.7490411
6.3000000 0.0168139 0.0168139 544.5719101 544.5719101 544.5719101
6.4000000 0.1165492 0.1165492 601.8450379 601.8450379 601.8450379
6.5000000 0.2151200 0.2151200 665.1416330 665.1416330 665.1416330
6.6000000 0.3115414 0.3115414 735.0951892 735.0951892 735.0951892
6.7000000 0.4048499 0.4048499 812.4058252 812.4058252 812.4058252
6.8000000 0.4941134 0.4941134 897.8472917 897.8472917 897.8472917
6.9000000 0.5784398 0.5784398 992.2747156 992.2747156 992.2747156
7.0000000 0.6569866 0.6569866 1096.6331584 1096.6331584 1096.6331584
7.1000000 0.7289690 0.7289690 1211.9670745 1211.9670745 1211.9670745
7.2000000 0.7936679 0.7936679 1339.4307644 1339.4307644 1339.4307644
7.3000000 0.8504366 0.8504366 1480.2999276 1480.2999276 1480.2999276
7.4000000 0.8987081 0.8987081 1635.9844300 1635.9844300 1635.9844300
7.5000000 0.9380000 0.9380000 1808.0424145 1808.0424145 1808.0424145
7.6000000 0.9679197 0.9679197 1998.1958951 1998.1958951 1998.1958951
7.7000000 0.9881682 0.9881682 2208.3479919 2208.3479919 2208.3479919
7.8000000 0.9985433 0.9985433 2440.6019776 2440.6019776 2440.6019776
7.9000000 0.9989413 0.9989413 2697.2823283 2697.2823283 2697.2823283
8.0000000 0.9893582 0.9893582 2980.9579870 2980.9579870 2980.9579870
8.1000000 0.9698898 0.9698898 3294.4680753 3294.4680753 3294.4680753
8.2000000 0.9407306 0.9407306 3640.9503073 3640.9503073 3640.9503073
8.3000000 0.9021718 0.9021718 4023.8723938 4023.8723938 4023.8723938
8.4000000 0.8545989 0.8545989 4447.0667477 4447.0667477 4447.0667477
8.5000000 0.7984871 0.7984871 4914.7688403 4914.7688403 4914.7688403
8.6000000 0.7343971 0.7343971 5431.6595914 5431.6595914 5431.6595914
8.7000000 0.6629692 0.6629692 6002.9122173 6002.9122173 6002.9122173
8.8000000 0.5849172 0.5849172 6634.2440063 6634.2440063 6634.2440063
8.9000000 0.5010209 0.5010209 7331.9735392 7331.9735392 7331.9735392
9.0000000 0.4121185 0.4121185 8103.0839276 8103.0839276 8103.0839276
9.1000000 0.3190984 0.3190984 8955.2927035 8955.2927035 8955.2927035
9.2000000 0.2228899 0.2228899 9897.1290587 9897.1290587 9897.1290587
9.3000000 0.1244544 0.1244544 10938.0192082 10938.0192082 10938.0192082
9.4000000 0.0247754 0.0247754 12088.3807302 12088.3807302 12088.3807302
9.5000000 -0.0751511 -0.0751511 13359.7268297 13359.7268297 13359.7268297
9.6000000 -0.1743268 -0.1743268 14764.7815656 14764.7815656 14764.7815656
9.7000000 -0.2717606 -0.2717606 16317.6071980 16317.6071980 16317.6071980
9.8000000 -0.3664791 -0.3664791 18033.7449278 18033.7449278 18033.7449278
9.9000000 -0.4575359 -0.4575359 19930.3704382 19930.3704382 19930.3704382
10.0000000 -0.5440211 -0.5440211 22026.4657948 22026.4657948 22026.4657948

Round very small numbers to zero (c++)

Is there any command in C++ to make,
1.354322e-23
into
0
This is my (simple) Program
#include "stdafx.h"
#include <iostream>
#include<iomanip>
int main()
{
float x;
std::cin >> x;
std::cout << x << std::endl;
return 0;
}
When I type values like,
2.2356e-17
It gives,
2.2356e-017
std::setprecision won't work either...
Edit:
OK this is my problem.
I created a program that can give sin cos and and tan values.
For cos 90, I want it to be 0 instead of -4.311e-008
Heres my real program
#include "stdafx.h"
#include <iostream>
#include<iomanip>
float Pi()
{
float pi = (atan(1) * 4);
return pi;
}
int Selector()
{
using namespace std;
cout << "Type:\t1 for Degrees\n\t2 for Radians\n\t3 for Gradians\n\nYour Choice : ";
int x;
cin >> x;
return x;
}
float D_R(float a)
{
float q = (a / 180);
float r = q*Pi();
return r;
}
float G_R(float a)
{
float q = (a / 200);
float r = q*Pi();
return r;
}
float All(float a, float o)
{
using namespace std;
std::cout << setprecision(5) << "sin(" << o << ") = " << sin(a) << std::endl;
std::cout << setprecision(5) << "cos(" << o << ") = " << cos(a) << std::endl;
std::cout << setprecision(5) << "tan(" << o << ") = " << tan(a) << std::endl;
return 0;
}
int main()
{
using namespace std;
int x = Selector();
cout << "Enter your angle : ";
float o;
cin >> o;
float d = D_R(o);
float g = G_R(o);
if (x == 1)
All(d, o);
else if (x == 2)
All(o, o);
else if (x == 3)
All(g, o);
return 0;
}
Edit:
Ok I came up with inserting
if (std::abs(sin(a)) < 0.0001) a = 0;
if (std::abs(cos(a)) < 0.0001) a = 0;
if (std::abs(tan(a)) < 0.0001) a = 0;
before my All() function
And that solved my problem
C++ can't arbitrarily round numbers down to 0 for you, it's up to you to define what a "very small number" is for your purposes.
Once you've determined the threshold, you simply need
if (std::abs(number) < THRESHOLD) number = 0;
RE: Your edits
For cos 90, I want it to be 0 instead of -4.311e-008
Again, it's up to you to define what the threshold is. Do you want 0.00000001 to be rounded to 0? What about 0.0001? What about 0.1? You need to define the line where rounding occurs.
Probably trunc() does what you want.
#include <cmath>
cout << roundf(2.2356e-17) << " " << trunc(2.2356e-17) << endl;
Output
0 0
See Also: round(), trunc(), nearbyint().

My c++ code for factoring isn't working

I am trying to create a c++ program that when I input two numbers (num1, combinationNum), it finds two numbers that multiply together to equal num1, but add together to equal combinationNum. It currently works for positive integers, but not negative. How do I make it work with negative integers? Also, If the equation isn't solvable, I would like it to print an error of some sort. Thanks!
Code:
//
// main.cpp
// Factor
//
// Created by Dani Smith on 2/13/14.
// Copyright (c) 2014 Dani Smith Productions. All rights reserved.
//
#include <iostream>
#include <cmath>
using namespace std;
void factors(int num, int comNum){
int a, b;
cout<<"The factors are ";
bool isPrime = true;
int root = (int)sqrt((double)num);
for(int i = 2; i <= root; i++){
if(num % i == 0 ){
isPrime = false;
//cout<<i<<",";
for(int x = 0; x<3; x++){
if(x==1){
a = i;
}
else if(x == 2){
b = i;
}
if(a + b == comNum){
cout << a << ", and " << b << ".";
}
}
}
}
//----------------------------------------
if(isPrime)cout<<"1 ";
cout<<endl;
}
int main(int argc, const char * argv[])
{
int num1 = 0, num2 = 0, multiple = 0, combinationNum = 0, output1 = 0, output2 = 0;
cout << "What number do you want to factor?\n";
cin >> num1;
cout << "What do you want them to add to?\n";
cin >> combinationNum;
factors(num1, combinationNum);
return 0;
}
To solve:
x + y == a
x * y == b
You have to solve
y == a - x
x * x - a * x + b == 0
So with delta == a * a - 4 * b, if delta positive, the solutions are
x1 = (a + sqrt(delta)) / 2
x2 = (a + sqrt(delta)) / 2
The code : (https://ideone.com/qwrSwa)
void solve(int sum, int mul)
{
std::cout << "solution for x + y = " << sum << std::endl
<< " x * y = " << mul << std::endl;
const int delta = sum * sum - 4 * mul;
if (delta < 0) {
std::cout << "No solution" << std::endl;
return;
}
const float sqrtdelta = sqrtf(delta);
const float x1 = (sum + sqrtdelta) / 2.f;
const float x2 = (sum - sqrtdelta) / 2.f;
std::cout << "x = " << x1 << ", y = " << sum - x1 << std::endl;
if (delta != 0) {
std::cout << "x = " << x2 << ", y = " << sum - x2 << std::endl;
}
}

Lowest double positive value - can cout change its value?

Consider two following pieces of code - the only difference between them is a single cout which prints the value eps:
http://ideone.com/0bEeHz - here the program enters and infinite loop since after the cout eps changes value to 0
#include <iostream>
int main()
{
double tmp = 1.;
double eps;
while(tmp != 0) {
eps = tmp;
tmp /= 2.;
}
if(eps == 0) {
std::cout << "(1)eps is zero!\n";
}
std::cout << "eps before: " << eps;
if(eps == 0) {
std::cout << "(2)eps is zero!\n";
}
while(eps < 1.) {
tmp = eps;
eps *= 2.;
if(tmp == eps) {
printf("wtf?\n");
}
}
std::cout << "eps after: " << eps;
}
http://ideone.com/pI4d30 - here I've commented out the cout.
#include <iostream>
int main()
{
double tmp = 1.;
double eps;
while(tmp != 0) {
eps = tmp;
tmp /= 2.;
}
if(eps == 0) {
std::cout << "(1)eps is zero!\n";
}
//std::cout << "eps before: " << eps;
if(eps == 0) {
std::cout << "(2)eps is zero!\n";
}
while(eps < 1.) {
tmp = eps;
eps *= 2.;
if(tmp == eps) {
printf("wtf?\n");
}
}
std::cout << "eps after: " << eps;
}
Hence, one single cout changes program logic dramatically and very surprisingly. Why is that?
I think it's a case of Section 5 (Expressions), paragraph 11
The values of the floating operands and the results of floating expressions may be represented in greater precision and range than that required by the type; the types are not changed thereby.
at work, cf. this variation of the original code.
while(tmp != 0) {
eps = tmp;
tmp /= 2.;
}
Calculations and comparisons performed at extended precision. The loop runs until eps is the smallest positive extended value (probably the 80-bit x87 extended type).
if(eps == 0) {
std::cout << "(1)eps is zero!\n";
}
Still at extended precision, eps != 0
std::cout << "eps before: " << eps;
For the conversion to a string to print, eps is stored and converted to double precision, resulting in 0.
if(eps == 0) {
std::cout << "(2)eps is zero!\n";
}
Yes, now it is.