add value_abs

This commit is contained in:
lixianjing 2022-10-26 11:50:09 +08:00
parent e14f7fa40c
commit 4f14c492b5
4 changed files with 105 additions and 0 deletions

View File

@ -1,5 +1,8 @@
# 最新动态
2022/10/26
* 增加函数value_abs
2022/10/24
* 修复designer中勾选旋转但编译参数无效的问题(感谢兆坤提供补丁)
* 完善how_to_use_fast_lcd_portrait.md文档(感谢雨欣提供补丁)

View File

@ -1648,3 +1648,77 @@ ret_t value_bit_xor(value_t* v, value_t* other, value_t* result) {
return ret;
}
ret_t value_abs(value_t* v, value_t* result) {
ret_t ret = RET_OK;
return_value_if_fail(v != NULL && result != NULL, RET_BAD_PARAMS);
switch (v->type) {
case VALUE_TYPE_BOOL: {
bool_t vv = value_bool(v);
value_set_bool(result, vv);
break;
}
case VALUE_TYPE_INT8: {
int8_t vv = value_int8(v);
value_set_int8(result, abs(vv));
break;
}
case VALUE_TYPE_UINT8: {
uint8_t vv = value_uint8(v);
value_set_uint8(result, abs(vv));
break;
}
case VALUE_TYPE_INT16: {
int16_t vv = value_int16(v);
value_set_int16(result, abs(vv));
break;
}
case VALUE_TYPE_UINT16: {
uint16_t vv = value_uint16(v);
value_set_uint16(result, abs(vv));
break;
}
case VALUE_TYPE_INT32: {
int32_t vv = value_int32(v);
value_set_int32(result, abs(vv));
break;
}
case VALUE_TYPE_UINT32: {
uint32_t vv = value_uint32(v);
value_set_uint32(result, abs(vv));
break;
}
case VALUE_TYPE_INT64: {
int64_t vv = value_int64(v);
value_set_int64(result, abs(vv));
break;
}
case VALUE_TYPE_UINT64: {
uint64_t vv = value_uint64(v);
value_set_uint64(result, abs(vv));
break;
}
case VALUE_TYPE_FLOAT32: {
uint64_t vv = value_float32(v);
value_set_float32(result, abs(vv));
break;
}
case VALUE_TYPE_FLOAT: {
uint64_t vv = value_float(v);
value_set_float(result, abs(vv));
break;
}
case VALUE_TYPE_DOUBLE: {
uint64_t vv = value_double(v);
value_set_double(result, abs(vv));
break;
}
default: {
ret = RET_BAD_PARAMS;
break;
}
}
return ret;
}

View File

@ -1047,6 +1047,16 @@ ret_t value_bit_and(value_t* v, value_t* other, value_t* result);
*/
ret_t value_bit_xor(value_t* v, value_t* other, value_t* result);
/**
* @method value_abs
* v取绝对值result对象
* @param {value_t*} v value对象()
* @param {value_t*} result value对象
*
* @return {ret_t} RET_OK表示成功
*/
ret_t value_abs(value_t* v, value_t* result);
END_C_DECLS
#endif /*TK_VALUE_H*/

View File

@ -1086,3 +1086,21 @@ TEST(value, bit_and_or_xor) {
ASSERT_EQ(value_bool(&r), FALSE);
}
TEST(value, abs) {
value_t v;
value_t o;
value_set_int(&v, -10);
ASSERT_EQ(value_abs(&v, &o), RET_OK);
ASSERT_EQ(o.type == v.type, TRUE);
ASSERT_EQ(value_int(&o), 10);
value_set_int8(&v, -10);
ASSERT_EQ(value_abs(&v, &o), RET_OK);
ASSERT_EQ(o.type == v.type, TRUE);
ASSERT_EQ(value_int8(&o), 10);
value_set_int64(&v, -10);
ASSERT_EQ(value_abs(&v, &o), RET_OK);
ASSERT_EQ(o.type == v.type, TRUE);
ASSERT_EQ(value_int64(&o), 10);
}