Questions tagged [negative-number]

A negative number is a number that is less than 0. It is signified by a preceding hyphen (i.e. -12,345).

This tag should be used for any questions dealing specifically with negative numbers.

599 questions
44
votes
3 answers

HashCode giving negative values

I am converting the incoming string into hash code by doing the following function but some of the values are negative. I don't think hash values should be negative. Please tell me what I am doing wrong. int combine = (srcadd + dstadd + sourceport +…
Xara
  • 8,748
  • 16
  • 52
  • 82
43
votes
6 answers

Right shifting negative numbers in C

I have C code in which I do the following. int nPosVal = +0xFFFF; // + Added for ease of understanding int nNegVal = -0xFFFF; // - Added for valid reason Now when I try printf ("%d %d", nPosVal >> 1, nNegVal >> 1); I get 32767 -32768 Is this…
Alphaneo
  • 12,079
  • 22
  • 71
  • 89
40
votes
4 answers

how to convert negative integer value to hex in python

I use python 2.6 >>> hex(-199703103) '-0xbe73a3f' >>> hex(199703103) '0xbe73a3f' Positive and negative value are the same? When I use calc, the value is FFFFFFFFF418C5C1.
nic nic
  • 413
  • 1
  • 4
  • 4
40
votes
2 answers

Why is the sign different after subtracting unsigned and signed?

unsigned int t = 10; int d = 16; float c = t - d; int e = t - d; Why is the value of c positive but e negative?
Eugene Kolombet
  • 331
  • 3
  • 5
40
votes
3 answers

How to change a negative number to zero in python without using decision structures

I have a program that determines the number of points you get per day, for 5 days from an event. source code: total=0 for x in range (5): points=int(input('How many points did you get today?')) total=total+points print ('You got {0}…
user1686896
  • 483
  • 1
  • 5
  • 9
38
votes
7 answers

Python Argparse: Issue with optional arguments which are negative numbers

I'm having a small issue with argparse. I have an option xlim which is the xrange of a plot. I want to be able to pass numbers like -2e-5. However this does not work - argparse interprets this is a positional argument. If I do -0.00002 it works:…
Ger
  • 958
  • 1
  • 8
  • 11
32
votes
2 answers

storing negative value in mysql

How can I store negative value in mysql decimal? I have data from DMS to Decimal having negative values, so it is decimal and negative. So what can I use to store such value?
Hafiz
  • 4,187
  • 12
  • 58
  • 111
27
votes
2 answers

Format Negative numbers in parenthesis BUT NOT with $ symbol?

I have seen all over the internet to format a NEGATIVE double value with a parenthesis WITH a $ symbol ie. currency type. I am looking for a .NET format string, to format 12345.67 = 12,345.67 -12345.67 = (12,345.67)
user715993
  • 291
  • 1
  • 5
  • 8
27
votes
3 answers

Java: right shift on negative number

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…
Cacheing
  • 3,431
  • 20
  • 46
  • 65
26
votes
4 answers

Representation of negative numbers in C?

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.
1s2a3n4j5e6e7v
  • 1,243
  • 3
  • 15
  • 29
22
votes
5 answers

Java Integer.MIN_VALUE's negative then compare yields two negatives

I have a test tomorrow and I can't understand my books explanation, I appreciate the help: public class TestClass{ public static void main(String[] args) throws Exception{ int a = Integer.MIN_VALUE; int b = -a; …
Quinma
  • 1,436
  • 2
  • 17
  • 39
21
votes
3 answers

C++: How can I return a negative value in main.cpp

As an assignment in school, we have to write a C++ program and returns different error codes in the main. The problem is that we have to return -2 if a specific error occurs but I have no idea how to return a negative value. For example: int…
Michi Kampl
  • 213
  • 2
  • 4
20
votes
5 answers

How to convert positive numbers to negative in Python?

I know that abs() can be used to convert numbers to positive, but is there somthing that does the opposite? I have an array full of numbers which I need to convert to negative: array1 = [] arrayLength = 25 for i in arrayLength: …
Aya Noaman
  • 334
  • 1
  • 2
  • 12
18
votes
2 answers

Create a random number between -100 and 100 in JavaScript?

Possible Duplicate: Generating random numbers in Javascript I have the following code var randomnumber=Math.floor(Math.random()*101); that generates a random number for me between 1 and 100. What I would like to do is generate a random number…
Cool Guy Yo
  • 5,910
  • 14
  • 59
  • 89
18
votes
3 answers

How to efficiently compare the sign of two floating-point values while handling negative zeros

Given two floating-point numbers, I'm looking for an efficient way to check if they have the same sign, given that if any of the two values is zero (+0.0 or -0.0), they should be considered to have the same sign. For instance, SameSign(1.0, 2.0)…
François Beaune
  • 4,270
  • 7
  • 41
  • 65
1
2
3
39 40