negative-number

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

2's complement hex number to decimal in java

元气小坏坏 提交于 2019-11-27 02:14:59
问题 I have a hex string that represents a 2's complement number. Is there an easy way (libraries/functions) to translate the hex into a decimal without working directly with its bits?? E.G. This is the expected output given the hex on the left: "0000" => 0 "7FFF" => 32767 (max positive number) "8000" => -32768 (max negative number) "FFFF" => -1 Thanks! 回答1: This seems to trick java into converting the number without forcing a positive result: Integer.valueOf("FFFF",16).shortValue(); // evaluates

Distinguish zero and negative zero

流过昼夜 提交于 2019-11-26 22:04:49
问题 I have run into a situation in my code where a function returns a double, and it is possible for this double to be a zero, a negative zero, or another value entirely. I need to distinguish between zero and negative zero, but the default double comparison does not. Due to the format of doubles, C++ does not allow for comparison of doubles using bitwise operators, so I am unsure how to procede. How can I distinguish between the two? 回答1: Call std::signbit() to determine the state of the sign

processing negative number in “accounting” format

此生再无相见时 提交于 2019-11-26 21:03:48
I have dataset, which negative value is presented with a bracket around the number i.e. (10)==-10 , it is in csv format, how can I process it so that R will interpret the (10) as -10 ? Thank you. UPDATE I know I can work it out by replacing ( as - , remove ) , and use as.numeric afterwards, but is there a more elegant way for this issue? If you create an "as.acntngFmt" method for the accounting format, you can read (or perhaps re-read with a text connection using colClasses("acnt"). setClass("acntngFmt") # [1] "acntngFmt" setAs("character", "acntngFmt", function(from) as.numeric( gsub("\\)", "

Java: right shift on negative number

陌路散爱 提交于 2019-11-26 17:34:57
I am very confused on right shift operation on negative number, here is the code. int n = -15; System.out.println(Integer.toBinaryString(n)); int mask = n >> 31; System.out.println(Integer.toBinaryString(mask)); And the result is: 11111111111111111111111111110001 11111111111111111111111111111111 Why right shifting a negative number by 31 not 1 (the sign bit)? Alvin Wong Because in Java there are no unsigned datatypes, there are two types of right shifts: arithmetic shift >> and logical shift >>> . http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html Arithmetic shift >> will keep

Floats not evaluating as negative (Python)

此生再无相见时 提交于 2019-11-26 17:22:07
问题 I am trying to delete floating point values in a list that are negative. The original list with all of the values looks like this: [ 0.030079979253112028, -0.006015995850622406, -0.08920269709543568, -25.72356846473029, -9.770807053941908, -66.38340248962655, -188.7778008298755, -165.95850622406638, 99.99, 33.81404564315352, 0.1742564315352697, -0.00560109958506224, -0.008297925311203318, -1.4044238589211617 ] After I run a for loop that says if num<0: list.remove(num) the list looks like

Representation of negative numbers in C?

纵饮孤独 提交于 2019-11-26 15:25:30
How does C represent negative integers? Is it by two's complement representation or by using the MSB (most significant bit)? -1 in hexadecimal is ffffffff . So please clarify this for me. ISO C ( C99 section 6.2.6.2/2 in this case but it carries forward to later iterations of the standard (a) ) states that an implementation must choose one of three different representations for integral data types, two's complement, ones' complement or sign/magnitude (although it's incredibly likely that the two's complement implementations far outweigh the others). In all those representations, positive

Best way to make Java&#39;s modulus behave like it should with negative numbers?

我的未来我决定 提交于 2019-11-26 15:05:42
In java when you do a % b If a is negative, it will return a negative result, instead of wrapping around to b like it should. What's the best way to fix this? Only way I can think is a < 0 ? b + a : a % b Peter Lawrey It behaves as it should a % b = a - a / b * b; i.e. it's the remainder. You can do (a % b + b) % b This expression works as the result of (a % b) is necessarily lower than b , no matter if a is positive or negative. Adding b takes care of the negative values of a , since (a % b) is a negative value between -b and 0 , (a % b + b) is necessarily lower than b and positive. The last

processing negative number in “accounting” format

≡放荡痞女 提交于 2019-11-26 07:49:44
问题 I have dataset, which negative value is presented with a bracket around the number i.e. (10)==-10 , it is in csv format, how can I process it so that R will interpret the (10) as -10 ? Thank you. UPDATE I know I can work it out by replacing ( as - , remove ) , and use as.numeric afterwards, but is there a more elegant way for this issue? 回答1: If you create an "as.acntngFmt" method for the accounting format, you can read (or perhaps re-read with a text connection using colClasses("acnt").

Java: right shift on negative number

南笙酒味 提交于 2019-11-26 06:06:38
问题 I am very confused on right shift operation on negative number, here is the code. int n = -15; System.out.println(Integer.toBinaryString(n)); int mask = n >> 31; System.out.println(Integer.toBinaryString(mask)); And the result is: 11111111111111111111111111110001 11111111111111111111111111111111 Why right shifting a negative number by 31 not 1 (the sign bit)? 回答1: Because in Java there are no unsigned datatypes, there are two types of right shifts: arithmetic shift >> and logical shift >>> .