Dividing double by 0 returns infinty [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
The code basically gives me infinty and the sting undefined. I only need undefined. I understand why its giving infinity but dont understand how to get rid of it. I cant use an exception as the error should only print undefined and nothing else.
double divide;
double a;
double b = 0;
if(args.length > 3){
System.out.println("Argument count mismatch");
}
else if(args.length == 1){
System.out.println("Argument count mismatch");
}
else{
a = Integer.parseInt(args[1]);
b = Integer.parseInt(args[2]);
divide = a / b;
System.out.println(divide);
if (b == 0) {
System.out.println("Undefined");
}
}

You can test the result with Double.isInfinite(double) (and you might also want to use Double.isNaN(double) and something like,
double a = 10;
double b = 0;
double divide = a / b;
if (Double.isInfinite(divide) || Double.isNaN(divide)) {
System.out.println("Undefined");
} else {
System.out.println(divide);
}
Output is
Undefined

Another way in addition to Elliott Frisch's way is to check for positive or negative infinity using the constants in the Double class.
if (divide == Double.POSITIVE_INFINITY || divide == Double.NEGATIVE_INFINITY)
{
System.out.println("Undefined");
}

Related

How can I count integer occurrence when the int is 0 in java? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm a beginner in java and I made a program to count the number of times an int d occurs in a given integer n i.e n = 988, d = 8 returns 2.
It works with most cases (negative, positive, etc.) but my code says n = 0, d = 1 contains 1, which is wrong. How do I add a place to my 0 without making the integer 10 (which I erroneously do in my first if statement).
public class countingints{
public static int count(int n, int d) {
n = Math.abs(n);
if(n == 0) {
n = 10;
}
int result = 0;
while (n > 0) {
int place = n % 10;
if (place == d) {
result++;
}
n /= 10;
}
return result;
}
public static void main(String[] args) {
System.out.println(count(0, 1)); //SHOULD return 0
System.out.println(count(0, 5)); //returns 0
}
}
if(n == 0) {
n = 10;
}
```
Computer just follows instructions. If n is 0, n is set to 10, and 10 contains a single 1 digit.
If perhaps your intent with this if (n == 0) n = 10 line is to ensure that e.g. count(0, 0) returns 1, then just code that in: if (n == 0 && d == 0) return 1; - that's a weird case because mathematically speaking the question 'how many times does the digit 0 show up' is tricky. You can write 15 as 0015 as well, and in many ways, 0 is just a way of writing it, the number really can be considered just a completely blank string with no digits at all.
Point is, it's logical, more or less, that your algorithm does 'weird things' when you ask it for how many zeroes are in a number, in particular when you ask it how many zeroes are in the number 0. "Weird cases" usually mean they need hardcoding, so, do that.

Why 1534236469 can't pass [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I think 1534236469 out of range! Leetcode: 7. Reverse Integer
I can't pass the test input 1534236469. why? the return range is[Integer.MAX_VALUE, Integer.MIN_VALUE],others should return zero
class Solution {
public int reverse(int x) {
if(x > Integer.MAX_VALUE || x < Integer.MIN_VALUE)
return 0;
int ans = 0;
while(x != 0) {
ans = ans * 10 + (x % 10);
x /= 10;
}
return ans;
}
}
Thank for your help
The reverse of 1534236469 is 9646324351, which is larger than Integer.MAX_VALUE, so your code will result in numeric overflow and an incorrect result.
You can use long instead of int to fix the problem.
EDIT:
Your added if(x > Integer.MAX_VALUE || x < Integer.MIN_VALUE) condition is pointless, since x is int, so it will never be outside the valid range of ints.
Even if x is within the valid range, the reverse of x may be outside the range. If you want to detect that the reversed x is too large and return 0, you should use long internally:
class Solution {
public int reverse(int x) {
long ans = 0;
while(x != 0) {
ans = ans * 10 + (x % 10);
x /= 10;
}
if(ans > Integer.MAX_VALUE || ans < Integer.MIN_VALUE) {
return 0;
} else {
return (int) ans;
}
}
}

Understanding code to find the largest prime factor of a number [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I found this code on an old youtube video and am having the hardest time understanding the logical sequence for the output. I was hoping any of you can help clarify.
Let's assume I pass a value of 21 to the getLargestPrime method.
In line 17 the conditional statement checks whether 21 % 2 != 0, that's True. So it carries another iteration of the code. But this time m = 3, which 21 % 3 != 0, that's actually False. Why is the code still executing the else statement? This is question #1.
Question #2
How can the if statement inside the else ever execute? When does number become == 1?
I know that this is probably super basic for you guys but for some reason I cannot follow the sequence in the output.
public class LargestPrime {
public static int getLargestPrime(int number){
if(number < 0){
return -1;
}
int m = 2;
int ans = 0;
int numbern;
if(number == 1){
System.out.println("This number is not a prime");
} else{
while (ans == 0){
if(number % m !=0){
m = m +1;
System.out.println(m + " value of m");
} else {
numbern = number;
number = number / m;
if(number == 1){
System.out.println(numbern + " is the largest prime factor of your number");
ans++;
}
}
}
} return number;
}
}
This is the output:
3 value of m
4 value of m
5 value of m
6 value of m
7 value of m
7 is the largest prime factor of your number
1
21%3!=0 returns false. Because of this the jvm executes the code after else. In e.g. 21%2!=0, that is true, it executes the code after if.
It is 1, if number is as m.
This is when your calculation gets to an end because there cannot be any more numbers after your number.

checking wether an int is not between 2 values [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How do i check wether a int is between two values when comparing it in an if statement, similar to this
if (num >= 1 && num <= 100 && bool != false) {System.out.print(true);}
Thanks.
Well, this looks like it should work. What are you looking for exactly - a more efficient way to do this?
int num =0;
if (num >= 1 && num <= 100) {
//do something
}
bool != false
is very confusing. Not only is it a double negative, but comparing a boolean value to true or false is redundant. Neither does the name give any hint as to its purpose. Something like the following would be clearer.
if(checkRange && (num >= 1 && num <= 100))
That question is quite weird since the answer is in the code you have provided. To check if the int is between two values just use this code.
if(num >= min && num <= max)
The more effective way - when you have multiple intervals is to use Interval Tree.
int j=3;
for (int i=0;i<=10;i++) {
if(j.equals(i)) {
System.out.println("Given value is between 0-10");
}
}

Multiply the digits at odd position by 1 and at even position by 3 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am doing a question and it wants me to get 12 digits to multiply. If the first digit is odd, it has to multiply by 1 otherwise the digit multiply by 3.My program is working. However, the coding is quite long. May I ask if there is any other method?
import java.util.Scanner;
public class ass2a
{
public static void main(String []args)
{
Scanner reader = new Scanner(System.in);
String input,b;
long checked;
System.out.print("Please enter the 12 digit:");
input = reader.nextLine();
long one,two,three,four,five,six,seven,eight,nine,ten,eleven,twevle;
checked =Long.parseLong(input);
one= checked / 100000000000L;
two = (checked % 100000000000L)/10000000000L;
three =(checked % 10000000000L)/ 1000000000L;
System.out.println(one);
System.out.println(two);
System.out.println(three);
if((input.length() < 12) || (input.length() > 12))
{
System.out.println("The entered digits is not equal to 12.");
System.exit(0);
}
else
{
System.out.println("Calculating...");
}
if (one % 2 ==0)
{
one = one * 1;
}
else
{
one = one * 3;
System.out.println("one is "+one);
}
if (two % 2 ==0)
{
two = two * 1;
}
else
{
two = two * 3;
System.out.println("two is "+two);
}
if (three % 2 ==0)
{
three = three * 1;
}
else
{
three = three * 3;
System.out.println("three is "+three);
}
System.out.println(one);
System.out.println(two);
System.out.println(three);
long sum = one +two+three;
System.out.println(sum);
}
}
Instead of creating individual variables, you could store the values in an array with length 12 and iterate that array:
long[] digits = new long[12];
// Add the input to the array
...
for(int i=0;i<digits.length;i++){
if(digits[i]%2==0){
digits[i] *= 3;
}
}
This is not complete code, but just an example to get you started. You can read up on Java arrays.

Categories

Resources