Get 1, 0, -1 as positive, zero, or negative for an integer with math only

Daniel Gimenez

I have a situation where I'm performing a calculate over a huge number of rows, and I can really increase the performance if I can eschew a conditional statement.

What I need is for a given positive, zero, or negative integer I want the result 1, 0, -1 respectively.

So if I do col/ABS(col), I will get 1 for a positive number, and -1 for a negative number, but of course if col equals 0 then I'll get an error. I can't get an error.

This seems simple enough, but I can't wrap my ahead around it.

biziclop

col/max(1, abs(col))

Ugly but works. For integers, that is. For floating point values where there's no well-defined smallest positive value, you're stuck unless the language allows you to look into it as a bit sequence, then you can just do the same with the sign flag and the significand.

Whether this helps optimising anything is highly debatable though. It certainly makes things harder to read.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SQL command to treat 0 as negative and 1 as positive

From Dev

Convert positive numbers and NaN to 1 and negative numbers to 0 in a specific column

From Dev

How get positive and negative number after 1 and 2 digit in PHP

From Dev

Normalize a negative or positive number between -1 and 1?

From Dev

Are Negative Zero And Positive Zero Denormalized?

From Dev

DocumentFilter for negative and positive integer

From Dev

AS3 convert a positive number to 1 and a negative number to -1

From Dev

Regex for only positive decimals from 0 to 1 upto 2 decimal digits only that works without sign in JavaScript?

From Java

Why is Math.pow(0, 0) === 1?

From Dev

Arrange array elements positive negative zero

From Java

How do I check if a zero is positive or negative?

From Dev

Arrange array elements positive negative zero

From Dev

Sorting an array that contains both positive and negative zero

From Dev

Java scanner won't accept negative integer values and only works with positive values

From Java

Given an integer N. What is the smallest integer greater than N that only has 0 or 1 as its digits?

From Dev

Redis HINCRBY hash field -1 counter for decrementing only till 0 or non negative value

From Dev

Redis HINCRBY hash field -1 counter for decrementing only till 0 or non negative value

From Dev

Integer equals 0 instead of 1

From Java

count negative and positive integer with while loop

From Dev

Change text colour depending on positive or negative integer

From Dev

A positive_integer/1 predicate that works for big numbers

From Java

Is (x - x) always positive zero for doubles, or sometimes negative zero?

From Dev

JavaScript - Detecting the difference between positive zero and negative zero

From Dev

How to display only the negative values, and make all the positive values display as 0?

From Dev

OWL: what is the difference between non-negative integer and positive integer?

From Dev

Why (positive_integer < negative_integer) is getting evaluated to be true?

From Dev

OWL: what is the difference between non-negative integer and positive integer?

From Dev

Mathematically checking if a number is positive, negative or 0

From Dev

bash allow only positive integer or positive integer with decimal point

Related Related

  1. 1

    SQL command to treat 0 as negative and 1 as positive

  2. 2

    Convert positive numbers and NaN to 1 and negative numbers to 0 in a specific column

  3. 3

    How get positive and negative number after 1 and 2 digit in PHP

  4. 4

    Normalize a negative or positive number between -1 and 1?

  5. 5

    Are Negative Zero And Positive Zero Denormalized?

  6. 6

    DocumentFilter for negative and positive integer

  7. 7

    AS3 convert a positive number to 1 and a negative number to -1

  8. 8

    Regex for only positive decimals from 0 to 1 upto 2 decimal digits only that works without sign in JavaScript?

  9. 9

    Why is Math.pow(0, 0) === 1?

  10. 10

    Arrange array elements positive negative zero

  11. 11

    How do I check if a zero is positive or negative?

  12. 12

    Arrange array elements positive negative zero

  13. 13

    Sorting an array that contains both positive and negative zero

  14. 14

    Java scanner won't accept negative integer values and only works with positive values

  15. 15

    Given an integer N. What is the smallest integer greater than N that only has 0 or 1 as its digits?

  16. 16

    Redis HINCRBY hash field -1 counter for decrementing only till 0 or non negative value

  17. 17

    Redis HINCRBY hash field -1 counter for decrementing only till 0 or non negative value

  18. 18

    Integer equals 0 instead of 1

  19. 19

    count negative and positive integer with while loop

  20. 20

    Change text colour depending on positive or negative integer

  21. 21

    A positive_integer/1 predicate that works for big numbers

  22. 22

    Is (x - x) always positive zero for doubles, or sometimes negative zero?

  23. 23

    JavaScript - Detecting the difference between positive zero and negative zero

  24. 24

    How to display only the negative values, and make all the positive values display as 0?

  25. 25

    OWL: what is the difference between non-negative integer and positive integer?

  26. 26

    Why (positive_integer < negative_integer) is getting evaluated to be true?

  27. 27

    OWL: what is the difference between non-negative integer and positive integer?

  28. 28

    Mathematically checking if a number is positive, negative or 0

  29. 29

    bash allow only positive integer or positive integer with decimal point

HotTag

Archive