From 825f043d8aab1821bc92893ca5d9d99cc1d94e3e Mon Sep 17 00:00:00 2001 From: h3x4n1um Date: Thu, 24 Oct 2019 23:08:34 +0700 Subject: [PATCH] Fix bug in inf, nan and small improve in number converter --- rton-json/include/RTON_number.hpp | 2 -- rton-json/include/rton-json.hpp | 2 +- rton-json/src/RTON_number.cpp | 35 +++++++++++-------------------- rton-json/src/json2rton.cpp | 4 ++-- rton-json/src/main.cpp | 4 ++-- 5 files changed, 17 insertions(+), 30 deletions(-) diff --git a/rton-json/include/RTON_number.hpp b/rton-json/include/RTON_number.hpp index cbba49c..52cd0d4 100644 --- a/rton-json/include/RTON_number.hpp +++ b/rton-json/include/RTON_number.hpp @@ -4,8 +4,6 @@ using namespace std; -constexpr double log256(double q); - uint64_t uRTON_t2uint64_t(vector q); vector uint64_t2uRTON_t(uint64_t q); diff --git a/rton-json/include/rton-json.hpp b/rton-json/include/rton-json.hpp index 76a632d..0f9be97 100644 --- a/rton-json/include/rton-json.hpp +++ b/rton-json/include/rton-json.hpp @@ -28,7 +28,7 @@ using json = nlohmann::basic_json; #endif const string architecture = ARCHITECTURE; -const string ver = "2.7.6"; +const string ver = "2.7.7"; extern ifstream input; extern ofstream output, debug; diff --git a/rton-json/src/RTON_number.cpp b/rton-json/src/RTON_number.cpp index 8486c70..3e66b37 100644 --- a/rton-json/src/RTON_number.cpp +++ b/rton-json/src/RTON_number.cpp @@ -4,38 +4,27 @@ #include "include/RTON_number.hpp" -constexpr double log256(double q){ - return log2(q) / 8; -} - vector uint64_t2uRTON_t(uint64_t q){ vector res; - if (q <= 0x7f){ - res.push_back(q); - return res; + while(q > 0){ + uint8_t temp = q % 0x100; + q = q / 0x100 * 2; + if (temp > 0x7f) ++q; + else if (q > 0) temp += 0x80; //reverse & 0x7f + res.push_back(temp); } - uint8_t temp = q % 0x100; - q = q / 0x100 * 2; - if (temp > 0x7f) ++q; - else temp += 0x80; //reverse & 0x7f - res = uint64_t2uRTON_t(q); - res.insert(res.begin(), temp); + if (res.empty()) res.push_back(0); return res; } uint64_t uRTON_t2uint64_t(vector q){ - if (q.size() == 1){ - if (q[0] > 0x7f) return UINT_MAX; //return max when RTON number has 1 byte and > 0x7f - else return q[0]; - } - uint64_t near_last_byte, last_byte = q[q.size() - 1]; - q.pop_back(); - while(q.size() > 0){ + if (q.size() == 1 && q[0] > 0x7f) return UINT64_MAX; //return max when RTON number has 1 byte and > 0x7f + uint64_t near_last_byte, last_byte = 0; + for (; q.size() > 0; q.pop_back()){ near_last_byte = q[q.size() - 1]; if (last_byte % 2 == 0) near_last_byte &= 0x7f; - near_last_byte += last_byte / 2 * 0x100; - last_byte = near_last_byte; - q.pop_back(); + last_byte /= 2; + last_byte = near_last_byte + last_byte * 0x100; } return last_byte; } diff --git a/rton-json/src/json2rton.cpp b/rton-json/src/json2rton.cpp index f7408d1..bdb1c03 100644 --- a/rton-json/src/json2rton.cpp +++ b/rton-json/src/json2rton.cpp @@ -96,13 +96,13 @@ int write_RTON_block(json js){ } } //nan - else if (temp.find("nan") != string::npos){ + else if (temp == "nan"){ double dnan = numeric_limits::signaling_NaN(); output.write(reinterpret_cast (&float64), sizeof float64); output.write(reinterpret_cast (&dnan), sizeof dnan); } //inf - else if (temp.find("inf") != string::npos){ + else if (temp == "inf" || temp == "-inf"){ double dinf = numeric_limits::infinity(); if (temp[0] == '-') dinf = -dinf; output.write(reinterpret_cast (&float64), sizeof float64); diff --git a/rton-json/src/main.cpp b/rton-json/src/main.cpp index b65439d..2978d97 100644 --- a/rton-json/src/main.cpp +++ b/rton-json/src/main.cpp @@ -149,8 +149,8 @@ int process_file(filesystem::path file_name, const int argc, const char *argv[]) int main(const int argc, const char *argv[]){ clog << endl << "rton-json made by H3x4n1um" << endl - << "Version: " << ver << endl - << architecture << " executable" << endl + << endl + << "Version: " << ver << " - " << architecture << " executable" << endl << "Compiled on " << __DATE__ << " at " << __TIME__ << endl << "Credits: nlohmann for his awesome JSON parser and fifo_map" << endl << endl;