Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file batNum.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113(*
* BatNum - Operations on arbitrary-precision numbers
* Copyright (C) 2008 Gabriel Scherer
* 2008 David Teller
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version,
* with the special exception on linking described in file LICENSE.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*)moduleBaseNum=structincludeNumtypet=numlet zero=Int0letone=Int1let neg=minus_numletabs=abs_numletadd=add_numletsub=sub_numletmul=mult_numletdiv=div_numletmodulo=mod_numletpow=power_numletcompare=compare_numletorder=BatOrd.ordcompareletequal=BatOrd.eq_compcompareletof_int =num_of_intletto_int=int_of_numletto_float=float_of_numletto_string=string_of_numletof_string=num_of_stringletpred=pred_numletsucc=succ_numletof_floatf=matchclassify_floatfwith|FP_normal|FP_subnormal->letx,e=frexpfinletn,e=Big_int.big_int_of_int64(Int64.of_float(ldexpx52)),(e-52)inife>=0thenBig_int(Big_int.shift_left_big_intne)elsediv(Big_intn)(Big_intBig_int.(shift_left_big_intunit_big_int~-e))|FP_zero->zero|FP_nan->divzerozero|FP_infinite->iff>=0.thendivonezeroelsediv(negone)zeroendmoduleTaggedInfix=structlet(=/),(</),(>/),(<=/),(>=/),(<>/)=Num.((=/),(</),(>/),(<=/),(>=/),(<>/))let (+/),(-/),(*/),(//),(**/ )=Num.((+/),(-/),(*/),(//),(**/ ))endmoduleInfix=struct(* infix operators without / suffix: + - * / *)includeBatNumber.MakeInfix(BaseNum)includeTaggedInfixendinclude(BatNumber.MakeNumeric(BaseNum):BatNumber.Numericwithtypet=Num.numandmoduleInfix:=Infix)includeNumletround=round_numletfloor=floor_numletceil=ceiling_numletsquare=square_numletis_integer=is_integer_numletapprox=integer_numletquo =quo_numletsign=sign_numletprintoutt=BatInnerIO.nwriteout(to_stringt)letof_float_stringa=tryletipart_s,fpart_s=BatString.splita~by:"."iniffpart_s=""thenof_stringipart_selseletfrac=pow(of_int10)(of_int(String.lengthfpart_s))indiv(of_string(ipart_s^fpart_s))fracwithNot_found->of_stringa(*$T
equal (of_float_string "2.5") (of_string "5/2")
equal (of_float_string "-2.5") (of_string "-5/2")
equal (of_float_string "-2.1") (of_string "-21/10")
equal (of_float_string "2.") (of_string "2")
equal (of_float_string ".5") (of_string "1/2")
equal (of_float_string "-0.5") (of_string "-1/2")
equal (of_float_string "-.5") (of_string "-1/2")
*)