123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2020 Nomadic Labs <contact@nomadic-labs.com> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)typefp_tag(* Tag for fixed point computations *)typeintegral_tag(* Tag for integral computations *)moduletypeSafe=sigtype'attypefp=fp_tagttypeintegral=integral_tagtvalintegral:Z.t->integralvalintegral_of_int:int->integralvalintegral_to_z:integral->Z.tvalzero:'atvaladd:'at->'at->'atvalsub:'at->'at->'atvalceil:fp->integralvalfloor:fp->integralvalfp:'at->fpval(=):'at->'bt->boolval(<>):'at->'bt->boolval(<):'at->'bt->boolval(<=):'at->'bt->boolval(>=):'at->'bt->boolval(>):'at->'bt->boolvalcompare:'at->'bt->intvalequal:'at->'bt->boolvalmax:'at->'at->'atvalmin:'at->'at->'atvalpp:Format.formatter->'at->unitvalpp_integral:Format.formatter->integral->unitvaln_fp_encoding:fpData_encoding.tvaln_integral_encoding:integralData_encoding.tvalz_fp_encoding:fpData_encoding.tvalz_integral_encoding:integralData_encoding.tendmoduletypeFull=sigincludeSafevalunsafe_fp:Z.t->fpendmoduletypeDecimals=sigvaldecimals:intendmoduleMake(Arg:Decimals):Full=structlet()=assert(Compare.Int.(Arg.decimals>=0))type'at=Z.tletscaling_factor=Z.pow(Z.of_int10)Arg.decimalstypefp=fp_tagttypeintegral=integral_tagtletintegralz=Z.mulzscaling_factorletintegral_of_intint=integral@@Z.of_intintletintegral_to_zx=Z.edivxscaling_factorletunsafe_fpx=xletzero=Z.zeroletadd=Z.addletsub=Z.subletceilx=letr=Z.eremxscaling_factorinifZ.equalrZ.zerothenxelseZ.addx(Z.subscaling_factorr)letfloorx=letr=Z.ediv_remxscaling_factor|>sndinifZ.equalrZ.zerothenxelseZ.subxrletfpx=xlet(=)=Compare.Z.(=)let(<>)=Compare.Z.(<>)let(<)=Compare.Z.(<)let(<=)=Compare.Z.(<=)let(>=)=Compare.Z.(>=)let(>)=Compare.Z.(>)letcompare=Z.compareletequal=Z.equalletmax=Compare.Z.maxletmin=Compare.Z.minletpp_positive_fpfmtrmilligas=ifCompare.Int.(Arg.decimals<>3)thenFormat.fprintffmtr"pp_positive_fp: cannot print (decimals <> 3)"elselet(q,r)=Z.ediv_remmilligasscaling_factorinifZ.equalrZ.zerothenZ.pp_printfmtrqelseFormat.fprintffmtr"%a.%03d"Z.pp_printq(Z.to_intr)letppfmtrfp=ifCompare.Z.(fp>=Z.zero)thenpp_positive_fpfmtrfpelseFormat.fprintffmtr"-%a"pp_positive_fp(Z.negfp)letpp_integral=ppletn_fp_encoding:fpData_encoding.t=Data_encoding.nletz_fp_encoding:fpData_encoding.t=Data_encoding.zletn_integral_encoding:integralData_encoding.t=Data_encoding.convintegral_to_zintegralData_encoding.nletz_integral_encoding:integralData_encoding.t=Data_encoding.convintegral_to_zintegralData_encoding.zend