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

Output of example in ReadMe file is not matching with the current master #2790

Open
faze-geek opened this issue May 30, 2024 · 2 comments
Open

Comments

@faze-geek
Copy link

Kindly checkout the Index Access example from https://github.com/xtensor-stack/xtensor?tab=readme-ov-file#basic-usage .

#include <iostream>
#include "xtensor/xarray.hpp"
#include "xtensor/xio.hpp"

int main(){
    xt::xarray<double> arr1
    {{1.0, 2.0, 3.0},
    {2.0, 5.0, 7.0},
    {2.0, 5.0, 7.0}};

    std::cout << arr1(0, 0) << std::endl;
}

Output

anurag@Anurags-MacBook-Air numpy % g++ -std=c++17 -o numpy src/numpy.cpp -Iinclude
anurag@Anurags-MacBook-Air numpy % ./numpy                                        
1           // wrong

1.0 is expected which is correctly shown in the ReadMe output.

I have also run the example on binder jupyter notebook to avoid any discrepancy caused locally.

@spectre-ns
Copy link
Contributor

I suspect this is a rounding issue in std::cout if you use the following line it displays correctly:

   std::cout << std::fixed << std::setprecision(1) << arr1(0, 0);

This is not an xtensor issue. 1 or 1.0 are equal and that is the correct output.

@faze-geek
Copy link
Author

I was not questioning the output, but with xtensor handling multiple dtypes getting 1. on print would be ideal. Let's make sure that the example prints in the same way as well to avoid any confusions.

It's a small refactor, maybe I can send in my first PR ?

  1. Update std::coutto use setprecision.
  2. Use a floating point number which won't be truncated like 1.5 instead of 1.0.
  3. Use xt::view(arr, 0, 0) for the indexing instead of arr(0, 0). This prints 1. as expected.

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

No branches or pull requests

2 participants