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

Owl_dataframe shouldn' t use 'string_of_float' #640

Open
edwintorok opened this issue May 23, 2023 · 1 comment
Open

Owl_dataframe shouldn' t use 'string_of_float' #640

edwintorok opened this issue May 23, 2023 · 1 comment

Comments

@edwintorok
Copy link
Contributor

'string_of_float' only prints 12 digits, which loses some digits, see discussion at ocaml/ocaml#11975.
Using %.17g instead should suffice for preserving all digits, however the numbers printed can have extra "ugly" unnecessary digits for commonly used values:

# Printf.sprintf "%.17g" 3.14;;
- : string = "3.1400000000000001"

To reproduce:

#require "owl";;
open Owl
let df = Dataframe.make ~data:[|Dataframe.pack_float_series [|1.;1. +. Float.epsilon|]|] [|"f"|] in Dataframe.to_csv df "/tmp/foo";; Dataframe.of_csv "/tmp/foo.csv";;
cat /tmp/foo.csv
f
1.
1.

https://ocaml.org/p/base/latest/doc/Base/Float/index.html#val-to_string has a pragmatic approach: try %.15g, and check that it round-trips, if not then use %.17g. See longer explanation at https://github.com/janestreet/base/blob/v0.15.0/src/float.ml#L68-L172

P.S.: Npy write/read doesn't have this problem: it preserves the float in its entirety.

@edwintorok
Copy link
Contributor Author

(there is also the '%h' printf format that prints floats in hex form, but that would'be interoperable in CSV, e.g. python wouldn't know to parse it by default in CSV and would leave it as a string)

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

1 participant