Skip to content

Commit

Permalink
Fix bug in inf, nan and small improve in number converter
Browse files Browse the repository at this point in the history
  • Loading branch information
h3x4n1um committed Oct 24, 2019
1 parent fdff5a0 commit 825f043
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 30 deletions.
2 changes: 0 additions & 2 deletions rton-json/include/RTON_number.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

using namespace std;

constexpr double log256(double q);

uint64_t uRTON_t2uint64_t(vector <uint8_t> q);

vector <uint8_t> uint64_t2uRTON_t(uint64_t q);
2 changes: 1 addition & 1 deletion rton-json/include/rton-json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ using json = nlohmann::basic_json<workaround_fifo_map>;
#endif
const string architecture = ARCHITECTURE;

const string ver = "2.7.6";
const string ver = "2.7.7";

extern ifstream input;
extern ofstream output, debug;
Expand Down
35 changes: 12 additions & 23 deletions rton-json/src/RTON_number.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,27 @@

#include "include/RTON_number.hpp"

constexpr double log256(double q){
return log2(q) / 8;
}

vector <uint8_t> uint64_t2uRTON_t(uint64_t q){
vector <uint8_t> 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 <uint8_t> 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;
}
4 changes: 2 additions & 2 deletions rton-json/src/json2rton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>::signaling_NaN();
output.write(reinterpret_cast<const char*> (&float64), sizeof float64);
output.write(reinterpret_cast<const char*> (&dnan), sizeof dnan);
}
//inf
else if (temp.find("inf") != string::npos){
else if (temp == "inf" || temp == "-inf"){
double dinf = numeric_limits<double>::infinity();
if (temp[0] == '-') dinf = -dinf;
output.write(reinterpret_cast<const char*> (&float64), sizeof float64);
Expand Down
4 changes: 2 additions & 2 deletions rton-json/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 825f043

Please sign in to comment.