添加float精确度接口

master
zcy 2022-01-05 16:22:02 +08:00
parent 72e20c74ae
commit 9a1f5494b3
6 changed files with 47 additions and 10 deletions

View File

@ -35,5 +35,8 @@ inline ENV_COMPILER CurrentEnvCompiler();
string itos(int x);
// 限制float精确度
float LimitFloat(float in,int size);
#endif //CUTILS_UTILS_H

View File

@ -1,6 +1,3 @@
//
// Created by 29019 on 2020/1/4.
//
#include "aes.h"
#include <cstring>

View File

@ -116,6 +116,7 @@ int Loger::Error(string dat,string function,int line) {
return 0;
}
int Loger::Warning(string dat,string function,int line) {
if(mLevel > LEVEL_WARNING) {
return 0;

View File

@ -62,3 +62,17 @@ inline ENV_COMPILER CurrentEnvCompiler()
}
// 限制float有效位
float LimitFloat(float in,int size){
uint64_t tmp = 1;
for(int i = 0;i < size;i++){
tmp = tmp*10;
}
uint64_t integer = uint64_t(in * tmp); // 提取整数部分
uint64_t decimal = integer %tmp; // 提取小数部分
std::cout<< "float_limit " << in<< " "<< integer << " "<< decimal << " "<< tmp <<std::endl;
return float(integer)/tmp;
}

View File

@ -17,8 +17,27 @@ typedef struct testsize2{
}Test2;
#pragma pack()
// 限制float有效位
float LimitFloat(float in,int size){
uint64_t tmp = 1;
for(int i = 0;i < size;i++){
tmp = tmp*10;
}
uint64_t integer = uint64_t(in * tmp); // 提取整数部分
uint64_t decimal = integer %tmp; // 提取小数部分
std::cout<< "float_limit " << in<< " "<< integer << " "<< decimal << " "<< tmp <<std::endl;
return float(integer)/tmp;
}
int main(){
std::cout<<sizeof(Test1) << " "<< sizeof(Test2)<<std::endl;
std::cout<< LimitFloat(1.123,2)<<std::endl;
std::cout<<LimitFloat(1.123,3) << std::endl;
std::cout<<LimitFloat(0.789,1) << std::endl;
std::cout<<LimitFloat(0,3) << std::endl;
std::cout<<"\r\n"<<std::endl<<sizeof(Test1) << " "<< sizeof(Test2)<<std::endl;
Test1 x = Test1{
1,2,3
};

View File

@ -58,11 +58,14 @@ void print_thread_id (int id) {
void TestLockGuard() {
std::thread threads[10];
// spawn 10 threads:
// spawn 10 threads:`
for (int i = 0; i < 10; ++i)
threads[i] = std::thread(print_thread_id,i+1);
threads[i] = std::thread(
print_thread_id,i+1
);
for (auto& th : threads) th.join();
for (auto& th : threads)
th.join();
return ;
}