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
17
votes
2 answers

How to calculate modulo of negative integers in JavaScript?

I'm trying to iterate over an array of jQuery objects, by incrementing or decrementing by 1. So, for the decrementing part, I use this code: var splitted_id = currentDiv.attr('id').split('_'); var indexOfDivToGo =…
jeff
  • 13,055
  • 29
  • 78
  • 136
16
votes
5 answers

How can I efficiently scrub Ruby's negative zero float?

In Ruby, 0.0 * -1 == -0.0. I have an application where I multiply a bunch of Float objects with -1, but I don't like the -0.0 in the output, since it's confusing. Is there a smart way of making Float#to_s output 0.0 instead of -0.0? I'm completely…
Frost
  • 11,121
  • 3
  • 37
  • 44
16
votes
8 answers

working with negative numbers in python

I am a student in a concepts of programming class. The lab is run by a TA and today in lab he gave us a real simple little program to build. It was one where it would multiply by addition. Anyway, he had us use absolute to avoid breaking the prog…
dman762000
  • 181
  • 1
  • 1
  • 6
15
votes
3 answers

How to check if a signed integer is neg or pos?

I am new to x86 assembly language, I have a signed integer saved in register eax, and I want to check if the number is negative or positive. To do that, I used bt instruction to check the first bit. Here is what I did: bt eax,0 jnc isNegative bt…
Yonk
  • 185
  • 1
  • 2
  • 6
15
votes
4 answers

How does Verilog behave with negative numbers?

For instance, say I have a reg [7:0] myReg I assign it the value -8'D69 I know Verilog stores it as 2's complement so it should be stored as 10111011 The question I have now is if I were to perform an operation on it, say myReg/2 Would it evaluate…
wonton
  • 7,568
  • 9
  • 56
  • 93
14
votes
3 answers

2's complement hex number to decimal in java

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"…
Marsellus Wallace
  • 17,991
  • 25
  • 90
  • 154
14
votes
7 answers

Random and negative numbers

I have to generate numbers in range [-100; +2000] in c++. How can I do this with rand if there is only positive numbers available? Are there any fast ways?
Max Frai
  • 61,946
  • 78
  • 197
  • 306
14
votes
2 answers

Why does slice [:-0] return empty list in Python

Stumbled upon something slightly perplexing today while writing some unittests: blah = ['a', 'b', 'c'] blah[:-3] # [] blah[:-2] # ['a'] blah[:-1] # ['a', 'b'] blah[:-0] # [] Can't for the life of me figure out why blah[:-0] # [] should be the case,…
Brent Hronik
  • 2,357
  • 1
  • 27
  • 43
14
votes
1 answer

How do I use negative numbers in a range in Swift?

I have the following code: switch self.score { case 1: self.score = self.score - 2 case -10...-10000: // ! Expected expression after unary operator println("lowest score") self.score = -10 …
webmagnets
  • 2,266
  • 3
  • 33
  • 60
14
votes
2 answers

Java - Is there a method for Euclidean or floored modulo

Java modulo operator % is based on the truncated division (see Wikipedia: Modulo operation). 5%3 produces 2 (note that 5/3 produces 1) 5%(-3) produces 2 (note that 5/(-3) produces -1) (-5)%3 produces -2 (note that (-5)/3 produces -1) (-5)%(-3)…
boumbh
  • 2,010
  • 1
  • 19
  • 21
14
votes
2 answers

How to iterate over range with negative step?

Is there a way to do something like: 0 to -10 map { i=> ... } repl gives me: scala.collection.immutable.IndexedSeq[Unit] = Vector()
virtualeyes
  • 11,147
  • 6
  • 56
  • 91
13
votes
2 answers

Convert positive value to negative value in swift

I want to convert a positive value to a negative value, for example: let a: Int = 10 turn it to -10, my current idea is just use it to multiple -1 a * -1 I'm not sure if this is proper, any idea?
William Hu
  • 15,423
  • 11
  • 100
  • 121
13
votes
5 answers

Are there more elegant ways to prevent negative numbers in Ruby?

Given that I'd like to do the following calculation: total = subtotal - discount Because discount might be greater than subtotal, there is code like the following: class Calculator def initialize(subtotal: subtotal, discount: discount) …
Domon
  • 6,753
  • 1
  • 25
  • 29
12
votes
1 answer

Why new[-1] generates segfault, while new[-2] throws bad_alloc?

I tried to test bad_alloc exception by passing some negative arguments to new[]. When passing small negative numbers I get what I hoped for - a bad_alloc. However, when passing -1, I can see that my object is constructed thousands of times (I print…
flyjohny
  • 273
  • 1
  • 2
  • 11
12
votes
1 answer

Is there a Delphi EncodeDate/DecodeDate function version that can handle B.C. dates?

The Delphi functions EncodeDate/DecodeDate seem to be able to handle only dates after 1.1.0001. Are there some implementations of EncodeDate/DecodeDate that can handle B.C. tDateTime values?
blerontin
  • 2,892
  • 5
  • 34
  • 60
1 2
3
39 40