absolute value / subtraction

absolute value examples
absolute value equations
absolute value formula
absolute value calculator
absolute value symbol
absolute value function
absolute value (algebra)
solving absolute value equations

I am trying to solve a problem that says:

Write a program that computes the difference between non-negative integers.


Each line of the input consists of a pair of integers. Each integer is between 0 and 10 raised to 15 (inclusive). The input is terminated by end of file.


For each pair of integers in the input, output one line, containing the absolute value of their difference.

Here is my solution:

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

int main(){
    int x;
    int y;
    int z;
    cin >> x >> y;
    if(x && y <= 1000000000000000){
        cout << std::abs (z);

But the problem is my answer is wrong, please help me correct my answer and explain why it was wrong.

For starters usually the range of valid values for objects of type int is less than 1000000000000000. You should use an integer type wide enough to store such big values. An appropriate type is unsigned long long int because according to the assignment entered values are non-negative.

Otherwise you will need also to check that the values are not negative that is greater than or equal to zero.

Also the condition in the if statement

if(x && y <= 1000000000000000){

is wrong. It is equivalent to

if(x && ( y <= 1000000000000000 )){

that in turn is equivalent to

if( ( x != 0 ) && ( y <= 1000000000000000 )){

it is not the same as

if ( ( x <= 1000000000000000 ) && ( y <= 1000000000000000 ) ){

The program can look the following way

#include <iostream>

int main()
    const unsigned long long int UPPER_VALUE = 1000000000000000;
    unsigned long long int x, y;

    while ( std::cin >> x >> y )
        if ( x <= UPPER_VALUE && y <= UPPER_VALUE )
            std::cout << "The difference between the numbers is "
                      << ( x < y ? y - x : x - y )
                      << std::endl;
            std::cout << "The numbers shall be less than or equal to " 
                      << UPPER_VALUE 
                      << std::endl;

If for example to enter these values

1000000000000000 1000000000000000 
1000000000000001 1000000000000000 
1 2

then the program output will look like

The difference between the numbers is 0
The numbers shall be less than or equal to 1000000000000000
The difference between the numbers is 1

Adding and Subtracting Absolute Value, Absolute value while adding and subtracting. signs you need to subtract the number then Duration: 2:34 Posted: 20 Aug 2014 Do the subtraction first: 9 – 13 = –4, which is negative, so the absolute value of –4 is 4. But the minus sign on the left (outside the absolute value bars in the original expression) negates this result, so the answer is –4.

Your if-condition is wrong as others mentioned already. Your variable x results to true for every value except zero. So you have to write (x <= MAX && y <= MAX).

Also you can prevent using fixed max values buy using the function numeric_limits (limits library):

#include <iostream>
#include <limits>
#include <cmath> 

int main() {
int x;
int y;
int z;

std::cin >> x >> y;

if (x <= std::numeric_limits<long>::max() && y <= std::numeric_limits<long>::max()) {
std::cout << std::abs (z);

What is Absolute Difference? (Absolute Value of Subtraction , Absolute value takes a negative number and makes it positive, and leaves other numbers Duration: 3:26 Posted: 16 Jul 2017 Evaluating Absolute Value Expressions. Recapitulate the concept of adding, subtracting, multiplying, and dividing the absolute value with this free printable worksheet. Each question here has two or more arithmetic operations involved. Use the PEMDAS rule to evaluate the absolute value expressions.

Your condition is written wrongly. It should have been

if(x <= 1000000000000000 && y <= 1000000000000000) {
    cout << std::abs (z);

However, your condition if(x && y <= 1000000000000000) evaluates in plain English to if x is true AND y is less than or equal to 1000000000000000, which is definitely not what you want. The way you have done, the condition will evaluate to true when y is less than 1000000000000000 AND x is any value other than zero, positive or negative. The reason behind this is in C any non-zero value evaluates to true.

Note: your code will fail when x and y are assigned any values not within the range of int which is -2^31 to 2^31-1 (considering int to be 32 bits, which is most common). You must use long long data type for x, y and z here.

long long x, y, z;

long long has range of -2^63 to 2^63-1, which is large enough to accommodate any number within the specified range, namely 0 to 1000000000000000.

Adding and Subtracting Absolute Value Tutorial, We explain Adding and Subtracting Absolute Value with video tutorials and quizzes, using our Many Ways(TM) approach from multiple teachers. This lesson � Add and subtract negative numbers, and see how absolute value can be used to find the distance between any two numbers on the number line. Our mission is to provide a free, world-class education to anyone, anywhere.

Absolute difference, The absolute difference of two real numbers x, y is given by |x − y|, the absolute value of their By contrast, simple subtraction is not non-negative or commutative, but it does obey the second and fourth properties above, since x − y = 0 if and� Absolute Value is the positive form of any negative value whether is it an integer number or decimal number. To use the ABS function, go to Insert function option from the Formula menu tab in Excel and select ABS function or else we can directly select ABS function by going in the edit mode of any cell.

Absolute value, In mathematics, the absolute value or modulus of a real number x, denoted |x|, is the non-negative value of x without regard to its sign. Namely, |x| = x if x is� An absolute value is not negative. It is the same as the original value but with no negative sign. The Math.Abs method in the .NET Framework provides a tested absolute value function. It deals with certain edge cases.

Negative Numbers and Absolute Value, Absolute Value; Adding and Subtracting Negative Numbers; Multiplying and Dividing Negative� To solve absolute value inequalities, isolate the absolute value expression, then solve the "positive version" of the inequality. Solve the "negative version" of the inequality by multiplying the quantity on the other side of the inequality by −1 and flipping the inequality sign.

  • Change if(x && y <= 1000000000000000) to if(x <= 1000000000000000 && y <= 1000000000000000). BTW, this condition is always true by definition (of int).
  • If x is zero, the if statement won't execute because 0 represents false.
  • Check the capacity of int on your system. You may need to use long or long long.
  • @barakmanos can you please elaborate a little what you meant by always true by definition of int? Notice that in the preceding line there is cin taking inputs for x and y
  • @Shadowfax: Sorry, by definition is most definitely the wrong terminology here. But most likely, the size of int on OP's platform is 32 bits, which is not sufficiently large in order to "accommodate" a value of 1000000000000000.
  • To accommodate values less than 10^15, unsigned long long is not needed. long long can well handle such numbers. Am I wrong?
  • @Shadowfax In the assignment there is written that the values are non-negative. If you are going to use a signed integer type then you also need to check the entered values with zero. What is the reason for makeing your life harder?:)
  • Why would I need to compare entered values with zero? I thought I am making my life easier by not writing unsigned word!
  • @Shadowfax You should follow the requirement of the assignment: "Write a program that computes the difference between non-negative integers." Otherwise your solution does not satisfy the assignment.
  • I don't want to argue, and I don't go along with your argument either. The assignment says the input numbers are going to be non-negative, it is just a piece of info given to me. Now it is up to me how I handle that non-negative number. I could if I wish handle that as a, say string, as long as it computes the difference. Actually, the OP is probably not an assignment, probably from some programming contests problem, like in UVa. I think I will just quit. It doesn't look good to argue with someone approx. 90 times you are, you know what I mean :-)
  • there is a bug where when the input is something about 10 raise to 10-14 something
  • The range of int is not necessarily what you say. It is at least 16 bits, and I don't think there's an upper limit.
  • @juanchopanza I have considered the int to be 32 bits, which I think is most common. Is not the range I said correct for 32 bit int? And how come there is no upper limit? BTW, I have run the code from here and it agrees with me.
  • The range may be correct for 32 bit int, but int isn't restricted to be 32 bits. It is specified to be at least as big as short, which is at least 16 bits. No upper limit is specified. And yes, these days 32 bit ints are very common.
  • Similarly, long long has range of at least -2^63 - 1 to 2^63 - 1. Yes, for most implementations it is, in fact, -2^63 to 2^63 - 1, but that extra value at the bottom is not required, and that range is the minimum.