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
3
votes
1 answer

The result of a modulo operation is negative

Why does the following C code produce negative numbers as output? And how do I prevent this from happening? #include int main() { int i; char buf[1024]; for (i = 0; i < 1024; i++) …
leonixyz
  • 1,130
  • 15
  • 28
3
votes
3 answers

Python Bit Shifting Negative vs positive values

I was recently going over bit shifting and was wondering why in the below iPython output, shifting the value -1 vs shifting the value 4294967295 yielded different results? In [30]: val = -1 In [31]: print "hex x %d 0x%08X" % (val, val &…
aaron-prindle
  • 3,077
  • 1
  • 17
  • 15
3
votes
1 answer

Difference of two imperial lengths

I have a program that takes two imperial lengths (miles, yards, feet and inches) and outputs their sum and difference. My problem is that when the difference is negative, the output is incorrect. def to_inches(miles, yards, feet, inches): return…
alexia
  • 14,440
  • 8
  • 42
  • 52
3
votes
3 answers

How to generate random numbers between -1 and 1 in C?

I am attempting Monte Carlo integration for an assignment. This involves generating random numbers between -1 and 1 in both the x and y axis... I think I know how to generate random nmumbers between 0 and 1, but don't know how to change that to…
user3368326
  • 51
  • 1
  • 3
3
votes
4 answers

apply negative value with variable using .css with jquery

I have a syntax issue as I want to do something quite simple. Apply a negative value to a variable using .css. Here yo have the code: var figureImage = $('.js-image-centering'); var figureImageHeight = figureImage.height(); var figureImageWidth =…
Daniel Ramirez-Escudero
  • 3,877
  • 13
  • 43
  • 80
3
votes
5 answers

Formatting negative numbers with brackets

I would like to format my negative numbers in "Accounting" format, i.e. with brackets. For example, I would like to format -1000000 as (1,000,000). I know the way of introducing thousands-separator: prettyNum(-1000000,…
Mayou
  • 8,498
  • 16
  • 59
  • 98
3
votes
5 answers

Convert negative .days to 0?

I am trying to compare the "end date" of a "contract" against datetime.now. Obviously, if todays date surpasses the contract end date then the value is returned as negative. I want a value that is negative to return as 0 while any end date that has…
Ger
  • 81
  • 1
  • 1
  • 6
3
votes
3 answers

Results of simple division change with a negative dividend

Calculating minutes (m) and hours (h) from seconds (s) works as I expect when s is positive: s = 14200 h = s/3600 #=> 3 m = (s % 3600 ) / 60 #=> 56 However, if s is a negative number, I get different results: s = -14200 h = s/3600 #=> -4 m = (s %…
aaandre
  • 2,502
  • 5
  • 33
  • 46
3
votes
3 answers

Printf comes out negative

I'm new to programming and am trying to have my program take a given number, double it, then continue to double for however many days the user input. I made this loop and it works but the final number comes out negative. I'm not sure how I can stop…
Shotgecko
  • 35
  • 6
3
votes
3 answers

android parsing negative number strings

How to parse negative number strings? strings may be of the form: -123.23 (123.23) 123.23- is there a class that will convert any of the above strings to a number? if not, what is the best way to do it?
quiricada
  • 79
  • 1
  • 5
2
votes
2 answers

If css("top") is or greater then -70 (negative value)

This is my piece of code which does not work. if ( $("div#verticalScrollbar").css("top") >= -70+"px" ) { alert("does work!"); } I literally WANT to say: If the top position is -70 or greater (like -71, -100, -444, etc.) then do the…
Tomkay
  • 5,120
  • 21
  • 60
  • 92
2
votes
2 answers

Why do I get -0 when dividing any number by -Infinity in JavaScript?

I was writing some JavaScript tests in Jest just to practice. I wrote the following function: const divide = (a, b) => { if (isNaN(a) || isNaN(b)) { throw new Error("This function expect 2 numbers"); } return a / b; }; Then I wrote the…
Chaiben
  • 23
  • 5
2
votes
2 answers

Dijkstra's Algorithm negative edge

Can someone help me with this question I am still confused weather Dijkstra's algorithm works with negative edges or not this question is from Grokking Algorithms book and in its errata it is said that this question has a possible answer how does…
YusufEmad04
  • 55
  • 1
  • 4
2
votes
3 answers

Convert a Negative Number with Parentheses to a Minus in MYSQL

I have a MYSQL database with Negative numbers that are enclosed in parenthesis eg. (14,500) which is supposed to be -14500. I am storing the numbers as varchar. I am trying to convert all the numbers to a double or float format and also format…
firstever
  • 49
  • 1
  • 6
2
votes
5 answers

Get printf to ignore the negative sign on values of zero

I'm trying to write a (mostly)* C program that sorts numerical results and eliminates duplicates. The results are stored as STRUCTS that contain a string, an integer, and 4 doubles. The doubles are what is relevant for determining if two results are…
BenB
  • 1,010
  • 12
  • 17