Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fsa_from_str #1287

Merged
merged 1 commit into from
May 17, 2024
Merged

Fix fsa_from_str #1287

merged 1 commit into from
May 17, 2024

Conversation

pkufool
Copy link
Collaborator

@pkufool pkufool commented May 16, 2024

Fix #1286

I think there should be something wrong with reading float from istringstream, here are some tests:

#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>

int main(){
  std::string str = "40000000 40000111 40000110 40000102 2147483647";
  std::istringstream iss(str);
  float aux;
  while (iss >> aux) {
    if ((int32_t)aux != aux) {
      std::cout << "require int" << std::endl;
    }
    std::cout << "float : " << std::setprecision (15) << aux << " int32 : " << (int32_t)aux << std::endl;
  }
}

The output is:

float : 40000000 int32 : 40000000
float : 40000112 int32 : 40000112
float : 40000112 int32 : 40000112
float : 40000104 int32 : 40000104
float : 2147483648 int32 : 2147483647

When changing to double :

#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>

int main(){
  std::string str = "40000000 40000111 40000110 40000102 2147483647";
  std::istringstream iss(str);
  double aux;
  while (iss >> aux) {
    if ((int32_t)aux != aux) {
      std::cout << "require int" << std::endl;
    }
    std::cout << "float : " << std::setprecision (15) << aux << " int32 : " << (int32_t)aux << std::endl;
  }
}

The output is:

float : 40000000 int32 : 40000000
float : 40000111 int32 : 40000111
float : 40000110 int32 : 40000110
float : 40000102 int32 : 40000102
float : 2147483647 int32 : 2147483647

I have not looked into the source code of standard library to figure out the deeper reasons, this is just a fix of the issue in k2.

@pkufool pkufool merged commit 43fe727 into k2-fsa:master May 17, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

aux labels id range limit?
2 participants