Num.num_of_string should accept decimal numbers

Aperta
#14 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
35/100
Tipo di issue
Funzionalità
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
ocaml
Ambito
backend

Direzione di ricerca

Start by locating Num.num_of_string and its existing integer-string parsing, then compare it with the sample implementation in the issue. Decimal strings such as "1.5" should parse while existing integer inputs retain their current behavior; verify both cases with the library's tests or interactive examples.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

feature wish

Original bug ID: 4553
Reporter: zack
Status: acknowledged (set by @damiendoligez on 2008-08-04T15:21:21Z)
Resolution: open
Priority: normal
Severity: feature
Version: 3.10.1
Category: otherlibs

Bug description

It seems that Num.num_of_string works only on strings denoting integers:

Num.num_of_string "1.5";;

Exception: Failure "num_of_string".

Supporting decimal numbers appears to be quite easy: numbers such as "1.5" can be mapped to the Ratio.ratio "15/10" and then injected to Num using the Num.Ratio constructor. The change will be backward compatible as the new functionality can be triggered only when the input string contain a '.', situation which currently raise the Failure above.

Can you please consider adding support for decimal numbers in the Num.num_of_string function?

A sample implementation (relying on the current implementation of Num.num_of_string for integer strings) is reported below in the additional information section.

Cheers.

Additional information

let my_num_of_string s =
let dot_pos = try Some (String.index s '.') with Not_found -> None in
match dot_pos with
| None -> Num.num_of_string s
| Some i -> (* e.g.: s = "45.6789", i = 2 )
let len = String.length s in
let numerator = (
e.g. "45" ^ "6789" = "456789" )
String.sub s 0 i ^ String.sub s (i + 1) (len - (i + 1)) in
let denominator = (
e.g. <big_int 10000> *)
Big_int.power_int_positive_int 10 (len - (i + 1)) in
Num.Ratio (Ratio.create_ratio (big_int_of_string numerator) denominator)

Lingua principale
OCaml
Stelle
39
Fork
29
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di ocaml/num

Tutte le issue di ocaml/num

Issue simili

Altre issue su Backend & API Design

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.