How to Find the Difference of Two Numbers and the Absolute Value of That Answer Without Using Math.Abs Function Java

How to find the difference of two numbers and the absolute value of that answer without using math.abs function JAVA

A bit more formatted code.

import java.util.Scanner;
class A {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double a;
double b;
System.out.println("Enter a: ");
a = in.nextDouble();

System.out.println("Enter b: ");
b = in.nextDouble();
double value = a - b;



//If value is negative...make it a positive number.
value = (value < 0) ? -value : value;
System.out.println("|"+a + "-" + b +"|" + " =" + value); // value should be printed here instead of (a-b) or (b-a)
System.out.println("|"+b + "-" + a +"|" + " =" + value);

}
}

Finding absolute value of a number without using Math.abs()

If you look inside Math.abs you can probably find the best answer:

Eg, for floats:

    /*
* Returns the absolute value of a {@code float} value.
* If the argument is not negative, the argument is returned.
* If the argument is negative, the negation of the argument is returned.
* Special cases:
* <ul><li>If the argument is positive zero or negative zero, the
* result is positive zero.
* <li>If the argument is infinite, the result is positive infinity.
* <li>If the argument is NaN, the result is NaN.</ul>
* In other words, the result is the same as the value of the expression:
* <p>{@code Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a))}
*
* @param a the argument whose absolute value is to be determined
* @return the absolute value of the argument.
*/
public static float abs(float a) {
return (a <= 0.0F) ? 0.0F - a : a;
}

How do I get the absolute value of an integer without using Math.abs?

You can use the conditional operator and the unary negation operator:

function absVal(integer) {
return integer < 0 ? -integer : integer;
}

get absolute value without using abs function nor if statement

From Bit Twiddling Hacks:

int v;           // we want to find the absolute value of v
unsigned int r; // the result goes here
int const mask = v >> sizeof(int) * CHAR_BIT - 1;

r = (v + mask) ^ mask;

Find the minimal absolute value of a sum of two elements

For input arrays e.g({-1, -2, -3}, {-1, -2}, {-1} your algorithm throws ArrayIndexOutOfBoundsException, so arrays when there are only negative numbers and there is no repeats

There is no chance to reach endless loop because either i or j change only + or - 1

How to get the difference between two integers

The result is 1 because compareTo() returns 0 if the arguments are equal, -1 if the first int is smaller than the second one and 1 if the second one is smaller (you can read more about it in the official docs).

--> You should not use this method for this purpose. Calculate the difference instead:

int x = pos2.x - pos1.x;
int y = pos2.y - pos1.y;
int size = Math.abs(x * y);

Max absolute difference of two max values at the different parts of the array?

In your program:

leftMax[k] holds the greatest value in A[0],...,A[k].

rightMax[k] holds the greatest value in A[k],...,A[n-1].

However, the right part should start at index k+1, not at k.

Therefore I suggest you change this part:

for (int k = 0; k<A.length; k++){
dif = Math.abs(leftMax[k] - rightMax[k]);
if (dif>maxDif) {
maxDif = dif;
}
}

to

for (int k = 0; k<A.length - 1; k++){
dif = Math.abs(leftMax[k] - rightMax[k + 1]);
if (dif>maxDif) {
maxDif = dif;
}
}

In other words, the requirement is to compute:

Math.Abs(Max(A[0], A[1]...A[K]) - Max(A[K+1], A[K+2]... A[N-1]))

but I believe your current program computes:

Math.Abs(Max(A[0], A[1]...A[K]) - Max(A[k], A[K+1], A[K+2]... A[N-1]))


Related Topics



Leave a reply



Submit