Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0004553OCamlOCaml generalpublic2008-05-08 15:022008-08-04 17:21
Reporterzack 
Assigned To 
PrioritynormalSeverityfeatureReproducibilityalways
StatusacknowledgedResolutionopen 
PlatformOSOS Version
Product Version3.10.1 
Target VersionFixed in Version 
Summary0004553: Num.num_of_string should accept decimal numbers
DescriptionIt 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 Informationlet 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)
TagsNo tags attached.
Attached Files

- Relationships

-  Notes
There are no notes attached to this issue.

- Issue History
Date Modified Username Field Change
2008-05-08 15:02 zack New Issue
2008-08-04 17:21 doligez Severity minor => feature
2008-08-04 17:21 doligez Status new => acknowledged
2008-08-04 17:21 doligez Additional Information Updated


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker