Merge pull request #33769 from bharatnc/ncb/degrees-radians-funcs

add DEGREES and RADIANS funcs
This commit is contained in:
Maksim Kita 2022-01-21 11:50:11 +01:00 committed by GitHub
commit ace483fb5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 223 additions and 0 deletions

View File

@ -477,3 +477,74 @@ Result:
└──────────┘
```
## degrees(x) {#degreesx}
Converts the input value in radians to degrees.
**Syntax**
``` sql
degrees(x)
```
**Arguments**
- `x` — Input in radians. [Float64](../../sql-reference/data-types/float.md#float32-float64).
**Returned value**
- Value in degrees.
Type: [Float64](../../sql-reference/data-types/float.md#float32-float64).
**Example**
Query:
``` sql
SELECT degrees(3.141592653589793);
```
Result:
``` text
┌─degrees(3.141592653589793)─┐
│ 180 │
└────────────────────────────┘
```
## radians(x) {#radiansx}
Converts the input value in degrees to radians.
**Syntax**
``` sql
radians(x)
```
**Arguments**
- `x` — Input in degrees. [Float64](../../sql-reference/data-types/float.md#float32-float64).
**Returned value**
- Value in radians.
Type: [Float64](../../sql-reference/data-types/float.md#float32-float64).
**Example**
Query:
``` sql
SELECT radians(180);
```
Result:
``` text
┌──────radians(180)─┐
│ 3.141592653589793 │
└───────────────────┘
```

30
src/Functions/degrees.cpp Normal file
View File

@ -0,0 +1,30 @@
#include <DataTypes/IDataType.h>
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionMathUnary.h>
namespace DB
{
namespace
{
struct DegreesName
{
static constexpr auto name = "degrees";
};
template <typename T>
Float64 degrees(T r)
{
Float64 degrees = r * (180 / M_PI);
return degrees;
}
using FunctionDegrees = FunctionMathUnary<UnaryFunctionVectorized<DegreesName, degrees>>;
}
void registerFunctionDegrees(FunctionFactory & factory)
{
factory.registerFunction<FunctionDegrees>(FunctionFactory::CaseInsensitive);
}
}

30
src/Functions/radians.cpp Normal file
View File

@ -0,0 +1,30 @@
#include <DataTypes/IDataType.h>
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionMathUnary.h>
namespace DB
{
namespace
{
struct RadiansName
{
static constexpr auto name = "radians";
};
template <typename T>
Float64 radians(T d)
{
Float64 radians = d * (M_PI / 180);
return radians;
}
using FunctionRadians = FunctionMathUnary<UnaryFunctionVectorized<RadiansName, radians>>;
}
void registerFunctionRadians(FunctionFactory & factory)
{
factory.registerFunction<FunctionRadians>(FunctionFactory::CaseInsensitive);
}
}

View File

@ -37,6 +37,8 @@ void registerFunctionSign(FunctionFactory & factory);
void registerFunctionMax2(FunctionFactory & factory);
void registerFunctionMin2(FunctionFactory & factory);
void registerVectorFunctions(FunctionFactory &);
void registerFunctionDegrees(FunctionFactory & factory);
void registerFunctionRadians(FunctionFactory & factory);
void registerFunctionsMath(FunctionFactory & factory)
@ -76,6 +78,8 @@ void registerFunctionsMath(FunctionFactory & factory)
registerFunctionMax2(factory);
registerFunctionMin2(factory);
registerVectorFunctions(factory);
registerFunctionDegrees(factory);
registerFunctionRadians(factory);
}
}

View File

@ -0,0 +1,42 @@
-360
-180.6
-180
-1
0
1
180
180.5
360
-6.283185307179586
-3.152064629101759
-3.141592653589793
-0.017453292519943295
0
0.017453292519943295
3.141592653589793
3.1503192998497647
6.283185307179586
-10
-6.283185307179586
-3.152064629101759
-3.141592653589793
-0.017453292519943295
0
0.017453292519943295
1
3.141592653589793
3.1503192998497647
6.283185307179586
10
-572.9577951308232
-360
-180.6
-180
-1
0
1
57.29577951308232
180
180.5
360
572.9577951308232

View File

@ -0,0 +1,46 @@
-- test conversion from degrees to radians
DROP TABLE IF EXISTS test_degs_to_rads;
CREATE TABLE test_degs_to_rads (degrees Float64) ENGINE = Memory;
INSERT INTO test_degs_to_rads VALUES (-1);
INSERT INTO test_degs_to_rads VALUES (-180);
INSERT INTO test_degs_to_rads VALUES (-180.6);
INSERT INTO test_degs_to_rads VALUES (-360);
INSERT INTO test_degs_to_rads VALUES (0);
INSERT INTO test_degs_to_rads VALUES (1);
INSERT INTO test_degs_to_rads VALUES (180);
INSERT INTO test_degs_to_rads VALUES (180.5);
INSERT INTO test_degs_to_rads VALUES (360);
-- test that converting degrees to radians and back preserves the original value
select DEGREES(RADIANS(degrees)) from test_degs_to_rads order by degrees;
-- test that radians func returns correct value for both int and floats
select RADIANS(degrees) from test_degs_to_rads order by degrees;
DROP TABLE test_degs_to_rads;
-- test conversion from radians to degrees
DROP TABLE IF EXISTS test_rads_to_degs;
CREATE TABLE test_rads_to_degs (radians Float64) ENGINE = Memory;
INSERT INTO test_rads_to_degs VALUES (-6.283185307179586);
INSERT INTO test_rads_to_degs VALUES (-3.152064629101759);
INSERT INTO test_rads_to_degs VALUES (-3.141592653589793);
INSERT INTO test_rads_to_degs VALUES (-0.017453292519943295);
INSERT INTO test_rads_to_degs VALUES(0);
INSERT INTO test_rads_to_degs VALUES(1);
INSERT INTO test_rads_to_degs VALUES(10);
INSERT INTO test_rads_to_degs VALUES(-10);
INSERT INTO test_rads_to_degs VALUES (0.017453292519943295);
INSERT INTO test_rads_to_degs VALUES (3.141592653589793);
INSERT INTO test_rads_to_degs VALUES (3.1503192998497647);
INSERT INTO test_rads_to_degs VALUES (6.283185307179586);
-- test that converting radians to degrees and back preserves the original value
select RADIANS(DEGREES(radians)) from test_rads_to_degs order by radians;
-- test that degrees func returns correct value for both int and floats
select DEGREES(radians) from test_rads_to_degs order by radians;
DROP TABLE test_rads_to_degs;