How to Determine Round-Off Error in a C++ Program?

  • Thread starter Thread starter ZachN
  • Start date Start date
  • Tags Tags
    Error Program
Click For Summary
SUMMARY

This discussion focuses on determining round-off error in a C++ program using user-defined inputs. The key formula for calculating the round-off error is defined as re = Abs(x-x*) <= 0.5x10(n-2), where x* is the rounded number. The provided C++ code attempts to implement this logic but contains several syntax errors, such as using 'count' instead of 'cout' and incorrect loop conditions. The discussion highlights the need for debugging and proper implementation of the round-off error calculation.

PREREQUISITES
  • Understanding of C++ syntax and structure
  • Familiarity with floating-point arithmetic and round-off error concepts
  • Knowledge of loops and conditional statements in C++
  • Experience with the cmath library for mathematical functions
NEXT STEPS
  • Debug the provided C++ code to correct syntax errors and ensure proper functionality
  • Research floating-point precision and its implications in numerical computing
  • Learn about the std::abs function in C++ for absolute value calculations
  • Explore best practices for error handling in C++ programs
USEFUL FOR

C++ developers, computer science students, and anyone interested in numerical methods and error analysis in programming.

ZachN
Messages
29
Reaction score
0

Homework Statement



Not a homework just self-teaching. I want to write a program which determines the round-off error of a number.

Homework Equations



re = Abs(x-x*)<= 0.5x10(n-2)

x*-0.5x10(n-2) < x < x* + 0.5x10(n-2)

The Attempt at a Solution



#include <iostream>
#include <cmath>
using namespace std;

int k, n; //Declares variables k and n.
double x, x_ro, sub_x, sup_x; //Declares variables x and x*.

int main()
{
count << "Enter a number.\n";
cin >> x;
count << "Enter the desired number of significant digits.\n";
cin>> k;
count << "Enter the rounded-off number.\n";
cin >> x_ro;

int n;
for (n == 0; n == -10; n--)
{
sub_x == x_ro - .5 * pow(10, n - k);
sup_x == x_ro + .5 * pow(10, n - k);
cin >> n;

if (x >= sub_x && x <= sup_x) //To check that condition is met
{

break;
count << "n = " << n;
}
}

for (n == 0; n == 10; n++)
{
sub_x == x_ro - .5 * pow(10, n - k);
sup_x == x_ro + .5 * pow(10, n - k);
cin >> n;

if (x >= sub_x && x <= sup_x)
{

break;
count << "n = " << n;
}

}

return 0;
}
 
Physics news on Phys.org
No one can offer even a suggestion? Maybe my code is a bit confusing.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
8
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 13 ·
Replies
13
Views
2K