division

How to divide 2 int in c?

若如初见. 提交于 2019-11-27 06:20:56
问题 wanna divide 2 numbers and get the result like this: 5 / 2 = 2.50 But it only outputs 2. I don't now what i'm doing wrong. Here my code: int a; int b; int c; printf("First num\n"); scanf("%d", &a); printf("Second num\n"); scanf("%d", &b); c = a / b; printf("%d", c); 回答1: You need a double variable to store the result. int stores only integers. Additionally, you have to typecast the other variables also before performing the division. Do something like this double c; . . . c = (double)a /

Why is Java's division broken?

丶灬走出姿态 提交于 2019-11-27 05:41:17
I am an experienced php developer just starting to learn Java. I am following some Lynda courses at the moment and I'm still really early stages. I'm writing sample programs that ask for user input and do simple calculation and stuff. Yesterday I came across this situation: double result = 1 / 2; With my caveman brain I would think result == 0.5 , but no, not in Java. Apparantly 1 / 2 == 0.0 . Yes, I know that if I change one of the operands to a double the result would also be a double. This scares me actually. I can't help but think that this is very broken. It is very naive to think that an

ArithmeticException thrown during BigDecimal.divide

元气小坏坏 提交于 2019-11-27 04:35:50
I thought java.math.BigDecimal is supposed to be The Answer™ to the need of performing infinite precision arithmetic with decimal numbers. Consider the following snippet: import java.math.BigDecimal; //... final BigDecimal one = BigDecimal.ONE; final BigDecimal three = BigDecimal.valueOf(3); final BigDecimal third = one.divide(three); assert third.multiply(three).equals(one); // this should pass, right? I expect the assert to pass, but in fact the execution doesn't even get there: one.divide(three) causes ArithmeticException to be thrown! Exception in thread "main" java.lang

When is the difference between quotRem and divMod useful?

旧街凉风 提交于 2019-11-27 04:02:34
问题 From the haskell report: The quot, rem, div, and mod class methods satisfy these laws if y is non-zero: (x `quot` y)*y + (x `rem` y) == x (x `div` y)*y + (x `mod` y) == x quot is integer division truncated toward zero, while the result of div is truncated toward negative infinity. For example: Prelude> (-12) `quot` 5 -2 Prelude> (-12) `div` 5 -3 What are some examples of where the difference between how the result is truncated matters? 回答1: Many languages have a "mod" or "%" operator that

BigInteger division in C#

强颜欢笑 提交于 2019-11-27 02:53:15
问题 I am writing a class which needs accurate division of the BigInteger class in C#. Example: BigInteger x = BigInteger.Parse("1000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); BigInteger y = BigInteger.Parse("2000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); x /= y; Console.WriteLine(x.ToString()); //Output = 0 The problem is that being an Integer, naturally it does not hold decimal values. How can I overcome this to

In Python, what is a good way to round towards zero in integer division?

旧街凉风 提交于 2019-11-27 02:38:44
问题 1/2 gives 0 as it should. However, -1/2 gives -1 , but I want it to round towards 0 (i.e. I want -1/2 to be 0), regardless of whether it's positive or negative. What is the best way to do that? 回答1: Do floating point division then convert to an int. No extra modules needed. >>> int(float(-1)/2) 0 >>> int(float(-3)/2) -1 >>> int(float(1)/2) 0 >>> int(float(3)/2) 1 回答2: Python's default division of integers is return the floor (towards negative infinity) with no ability to change that. You can

What is purpose of the div() library function?

你说的曾经没有我的故事 提交于 2019-11-27 02:23:41
When c has the / operator to divide two numbers, what is the purpose of having the div() library function ? Is there any scenario where / can't be used but div() can? From the C99 Rationale document: (7.20.6.2 The div, ldiv, and lldiv functions) Because C89 had implementation-defined semantics for division of signed integers when negative operands were involved, div and ldiv, and lldiv in C99, were invented to provide well-specified semantics for signed integer division and remainder operations. The semantics were adopted to be the same as in Fortran. Since these functions return both the

python: getting around division by zero

天涯浪子 提交于 2019-11-27 02:14:53
问题 I have a big data set of floating point numbers. I iterate through them and evaluate np.log(x) for each of them. I get RuntimeWarning: divide by zero encountered in log I would like to get around this and return 0 if this error occurs. I am thinking of defining a new function: def safe_ln(x): #returns: ln(x) but replaces -inf with 0 l = np.log(x) #if l = -inf: l = 0 return l Basically,I need a way of testing that the output is -inf but I don't know how to proceed. Thank you for your help! 回答1

C integer division and floor

旧时模样 提交于 2019-11-27 01:40:32
问题 In C, is there a difference between integer division a/b and floor(a/b) where both a and b are integers? More specifically what happens during both processes? 回答1: a/b does integer division. If either a or b is negative, the result depends on the compiler (rounding can go toward zero or toward negative infinity in pre-C99; in C99+, the rounding goes toward 0). The result has type int . floor(a/b) does the same division, converts the result to double, discards the (nonexistent) fractional part

Find multiples of a number in PHP

北战南征 提交于 2019-11-27 01:31:47
问题 I want to find all muliples of a number in PHP. I'm using something like this if($count != 20 ) to work out if $count is not equal to 20. But I also need this script to check if $count is not equal to 20, 40, 60, 80, 100, 120, 140, 160 etc. Any ideas? I think i need to use the modulus symbol ( % ), but I don't know. 回答1: if ($count % 20 != 0) 回答2: if ($count % 20 != 0) { // $count is not a multiple of 20 } 回答3: If you don't want zero to be excluded: if ($count % 20 != 0 || $count == 0) 回答4: