Ash has a bit of an interesting float division algorithm. It's designed to never return NaN
, and things like signed zero and infinity need to be handled.
How it works:
Assume the inputs are positive for the rules below. One input being negative will always result in the output being negative, and both being negative will result in a positive output.
- Zero divided by anything is
0
- Anything other than
0
divided by0
isInfinity
- Anything other than
Infinity
divided byInfinity
is0
Infinity
divided by anything other thanInfinity
isInfinity
Infinity
divided byInfinity
is1
- All other division works as normal
I/O:
For languages with floats that support signed zero and positive/negative infinity, floats are an accepted I/O format. For others, using a string is allowed. You can use any reasonable format in this string, and you can represent the infinities with anything that can't be confused with another number (such as I
, ∞
, inf
, or :(
).
Input will always be two floats in your chosen representation, and output will be a single one. Floating point errors and large numbers being represented as Infinity
are allowed.
Test cases:
0 / 0 0
0 / 1 0
0 / 8 0
0 / Infinity 0
0 / -0 -0
0 / -1 -0
0 / -8 -0
0 / -Infinity -0
-0 / 0 -0
-0 / 1 -0
-0 / 8 -0
-0 / Infinity -0
-0 / -0 0
-0 / -1 0
-0 / -8 0
-0 / -Infinity 0
1 / 0 Infinity
8 / 0 Infinity
Infinity / 0 Infinity
1 / -0 -Infinity
8 / -0 -Infinity
Infinity / -8 -Infinity
-1 / 0 -Infinity
-8 / 0 -Infinity
-Infinity / 0 -Infinity
-1 / -0 Infinity
-8 / -0 Infinity
-Infinity / -0 Infinity
Infinity / 1 Infinity
Infinity / 8 Infinity
Infinity / -1 -Infinity
Infinity / -8 -Infinity
-Infinity / 1 -Infinity
-Infinity / 8 -Infinity
-Infinity / -1 Infinity
-Infinity / -8 Infinity
Infinity / Infinity 1
Infinity / -Infinity -1
-Infinity / Infinity -1
-Infinity / -Infinity 1
1 / 1 1
1 / 8 0.125
1 / Infinity 0
1 / -1 -1
1 / -8 -0.125
1 / -Infinity -0
-1 / 1 -1
-1 / 8 -0.125
-1 / Infinity -0
-1 / -1 1
-1 / -8 0.125
-1 / -Infinity 0
8 / 1 8
8 / 8 1
8 / Infinity 0
8 / -1 -8
8 / -8 -1
8 / -Infinity -0
-8 / 1 -8
-8 / 8 -1
-8 / Infinity -0
-8 / -1 8
-8 / -8 1
-8 / -Infinity 0
1 / 0.125 8
0.1 / 0.2 0.5
10 / -3 -3.333333
1 / 0.0000000000000001 10000000000000000
Other:
This is code-golf, so shortest answer in bytes (per language) wins!
[sign, positive float]
a reasonable representation? \$\endgroup\$/
which works fine :p \$\endgroup\$