-
Notifications
You must be signed in to change notification settings - Fork 4k
ARROW-9388: [C++] Division kernels #7748
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -492,6 +492,88 @@ TYPED_TEST(TestBinaryArithmeticFloating, Add) { | |
| this->AssertBinop(Add, "[null, 2.0]", this->MakeNullScalar(), "[null, null]"); | ||
| } | ||
|
|
||
| TYPED_TEST(TestBinaryArithmeticFloating, Div) { | ||
| for (auto check_overflow : {false, true}) { | ||
| this->SetOverflowCheck(check_overflow); | ||
| // Empty arrays | ||
| this->AssertBinop(Divide, "[]", "[]", "[]"); | ||
| // Ordinary arrays | ||
| this->AssertBinop(Divide, "[3.4, 0.64, 1.28]", "[1, 2, 4]", "[3.4, 0.32, 0.32]"); | ||
| // Array with nulls | ||
| this->AssertBinop(Divide, "[null, 1, 3.3, null, 2]", "[1, 4, 2, 5, 0.1]", | ||
| "[null, 0.25, 1.65, null, 20]"); | ||
| // Scalar divides by array | ||
| this->AssertBinop(Divide, 10.0F, "[null, 1, 2.5, null, 2, 5]", | ||
| "[null, 10, 4, null, 5, 2]"); | ||
| // Array divides by scalar | ||
| this->AssertBinop(Divide, "[null, 1, 2.5, null, 2, 5]", 10.0F, | ||
| "[null, 0.1, 0.25, null, 0.2, 0.5]"); | ||
| // Array with infinity | ||
| this->AssertBinop(Divide, "[3.4, Inf, -Inf]", "[1, 2, 3]", "[3.4, Inf, -Inf]"); | ||
| // Scalar divides by scalar | ||
| this->AssertBinop(Divide, 21.0F, 3.0F, 7.0F); | ||
| } | ||
| } | ||
|
|
||
| TYPED_TEST(TestBinaryArithmeticIntegral, Div) { | ||
| for (auto check_overflow : {false, true}) { | ||
| this->SetOverflowCheck(check_overflow); | ||
|
|
||
| // Empty arrays | ||
| this->AssertBinop(Divide, "[]", "[]", "[]"); | ||
| // Ordinary arrays | ||
| this->AssertBinop(Divide, "[3, 2, 6]", "[1, 1, 2]", "[3, 2, 3]"); | ||
| // Array with nulls | ||
| this->AssertBinop(Divide, "[null, 10, 30, null, 20]", "[1, 4, 2, 5, 10]", | ||
| "[null, 2, 15, null, 2]"); | ||
| // Scalar divides by array | ||
| this->AssertBinop(Divide, 33, "[null, 1, 3, null, 2]", "[null, 33, 11, null, 16]"); | ||
| // Array divides by scalar | ||
| this->AssertBinop(Divide, "[null, 10, 30, null, 2]", 3, "[null, 3, 10, null, 0]"); | ||
| // Scalar divides by scalar | ||
| this->AssertBinop(Divide, 16, 7, 2); | ||
| } | ||
| } | ||
|
|
||
| TYPED_TEST(TestBinaryArithmeticSigned, Div) { | ||
| // Ordinary arrays | ||
| this->AssertBinop(Divide, "[-3, 2, -6]", "[1, 1, 2]", "[-3, 2, -3]"); | ||
| // Array with nulls | ||
| this->AssertBinop(Divide, "[null, 10, 30, null, -20]", "[1, 4, 2, 5, 10]", | ||
| "[null, 2, 15, null, -2]"); | ||
| // Scalar divides by array | ||
| this->AssertBinop(Divide, 33, "[null, -1, -3, null, 2]", "[null, -33, -11, null, 16]"); | ||
| // Array divides by scalar | ||
| this->AssertBinop(Divide, "[null, 10, 30, null, 2]", 3, "[null, 3, 10, null, 0]"); | ||
| // Scalar divides by scalar | ||
| this->AssertBinop(Divide, -16, -8, 2); | ||
| } | ||
|
|
||
| TYPED_TEST(TestBinaryArithmeticIntegral, DivideByZero) { | ||
| for (auto check_overflow : {false, true}) { | ||
| this->SetOverflowCheck(check_overflow); | ||
| this->AssertBinopRaises(Divide, "[3, 2, 6]", "[1, 1, 0]", "divide by zero"); | ||
| } | ||
| } | ||
|
|
||
| TYPED_TEST(TestBinaryArithmeticFloating, DivideByZero) { | ||
| for (auto check_overflow : {false, true}) { | ||
| this->SetOverflowCheck(check_overflow); | ||
| this->AssertBinopRaises(Divide, "[3.0, 2.0, 6.0]", "[1.0, 1.0, 0]", "divide by zero"); | ||
| } | ||
| } | ||
|
|
||
| TYPED_TEST(TestBinaryArithmeticSigned, DivideOverflowRaises) { | ||
| using CType = typename TestFixture::CType; | ||
| auto min = std::numeric_limits<CType>::lowest(); | ||
|
|
||
| this->SetOverflowCheck(true); | ||
| this->AssertBinopRaises(Divide, MakeArray(min), MakeArray(-1), "overflow"); | ||
|
||
|
|
||
| this->SetOverflowCheck(false); | ||
| this->AssertBinop(Divide, MakeArray(min), MakeArray(-1), "[0]"); | ||
| } | ||
|
|
||
| TYPED_TEST(TestBinaryArithmeticFloating, Sub) { | ||
| this->AssertBinop(Subtract, "[]", "[]", "[]"); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your suggestion.
According to the IEEE 754 standard, dividing by zero should be well-defined for floating point numbers. Please see, for example, https://dl.acm.org/doi/10.1145/103162.103163, page 26.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is true, but division by zero can result in
SIGFPEiffeenableexcept()or a hardware exception if_control87()are used. For example:These are rare, so maybe we don't need to support them (or at least restrict this check to the checked division kernel), but they do represent a potential crash when dividing by
0.0. @pitrou ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd say if the user really wants to enable FP exceptions process-wise, we shouldn't try to counteract them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, I withdraw this suggestion