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
6
votes
3 answers

Reject Negative Numbers as exceptions in Python

I'm trying to run a basic prompt that takes a number, then runs a recursive function on it. Any negative number causes a recursion error, for the function being unable to handle them. Now, I've learned with Python that situations like this call for…
Mark Puchala II
  • 634
  • 2
  • 8
  • 25
6
votes
3 answers

Rounding a Positive or Negative Number

I need to do rounding on a number, but I don't know whether that number is negative or positive. Is there a better way to round foo that to do this: static_cast(foo > 0 ? foo + 0.5 : foo - 0.5) Basically I want this behavior: 3.4 => 3 3.5…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
6
votes
3 answers

Why do Excel values in parentheses become negative values?

A colleague and I encountered a behavior in Excel which isn't clear to us. Background: We have a tool which converts an Excel sheet into a table format. The tool calculates the formulas which are in excel and replaces variables inside it with…
Bongo
  • 2,933
  • 5
  • 36
  • 67
6
votes
2 answers

Negative zero in JavaScript?

I've just noticed that I can do the following in JavaScript: var z = -0; console.log(z); // prints -0 Why does the unary negation works on zero? Is this one of the many JavaScript quirks or it does (somehow) have a purpose? P.S.: It seems to be…
talles
  • 14,356
  • 8
  • 45
  • 58
6
votes
3 answers

OCaml: Matching with any negative

Is there a way to get pattern matching to match my value with any negative number? It does not matter what the negative number is I just need to match with any negative. I have accomplished what I want with this simple code: let y = if(n < 0) then…
nicotine
  • 2,519
  • 3
  • 19
  • 15
6
votes
1 answer

Python Range() for positive and negative numbers

Okay, so I'm trying to write a simple program that gives me both the positive and negative range of the number given by the user. For example, if the user gives number 3 then the program should prints out -3 -2 -1 0 1 2 3 I've tried thinking but…
6
votes
2 answers

PHP Unsigned Right Shift - Malfunctioning

So, when using my method to preform a ( >>> ) unsigned right shift in PHP, the result is incorrect when the numbers involve negatives. PHP Application Results: INPUT: 10 >>> 3 INPUT: -10 >>> 3 OUTPUT: 1 OUTPUT: 2684354558 JAVA APPLICATION…
Mitchell M
  • 475
  • 1
  • 7
  • 15
5
votes
1 answer

C checking for signed number representation

The following way of checking for the signed number representation checks for twos complement correctly on my machine, but I dont have ones complement or signed magnitude machines to check it. Would the code work properly and more importantly, is it…
tyty
  • 839
  • 5
  • 12
5
votes
3 answers

Compilers and negative numbers representations

Recently I was confused by this question. Maybe because I didn't read language specifications (it's my fault, I know). C99 standard doesn't say which negative numbers representation should be used by compiler. I always thought that the only right…
klew
  • 14,837
  • 7
  • 47
  • 59
5
votes
3 answers

Pandas DataFrame replace negative values with latest preceding positive value

Consider a DataFrame such as df = pd.DataFrame({'a': [1,-2,0,3,-1,2], 'b': [-1,-2,-5,-7,-1,-1], 'c': [-1,-2,-5,4,5,3]}) For each column, how to replace any negative value with the last positive value or zero…
iris
  • 379
  • 1
  • 2
  • 9
5
votes
1 answer

It there a way in powershell to make a positive number to a negative number whitout using multiplication?

I was wondering if there is a way to make a positive number into a negative number whitout using a multiplication like $b = $a * -1 I'm looking for the most cost sensible way because I'm gonna do this a lot of times in a script. -edit At this point…
hexedecimal
  • 279
  • 1
  • 2
  • 9
5
votes
3 answers

Python sorting list with negative number

In attempt to learn python by practicing, I am trying to implement and test out quick sort algorithm using python. The implementation itself was not difficult, however the result of the sort is somewhat puzzling: When I sort a list ['35', '-1',…
Ishiro Kusabi
  • 211
  • 2
  • 6
  • 13
5
votes
4 answers

How do I remove leading zeroes for a negative number

How can I remove leading zeroes on negative numbers in C#? For example, I wish '-01' to convert to '-1'.
Taewan
  • 1,167
  • 4
  • 15
  • 25
5
votes
1 answer

Why can't a negative normalized floating point binary number start with 11?

Studying for A level computing we are repeatedly told that a negative normalised floating point binary number is not normalised if it starts with 11 by textbooks, exam questions and teachers. In the case of minus 11 in twos compliment it can be…
5
votes
1 answer

Retrieve largest negative number and smallest positive number from list

Given a list of integers, e.g.: lst = [-5, -1, -13, -11, 4, 8, 16, 32] is there a Pythonic way of retrieving the largest negative number in the list (e.g. -1) and the smallest positive number (e.g. 4) in the list?
user3191569
  • 485
  • 1
  • 7
  • 24