Skip to content

Commit

Permalink
Add some more examples and details
Browse files Browse the repository at this point in the history
Added examples for trigonometric functions and exceptions. Added some more details for trigonometric functions.
  • Loading branch information
vikram2000b committed Sep 23, 2020
1 parent d0e03e4 commit 548dec5
Showing 1 changed file with 148 additions and 3 deletions.
151 changes: 148 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ The boost::real::const_precision_iterator is a forward iterator [4] that iterate
>
>(5) - (10) **Trigonometric functions**
> These are trigonometric functions, and their name corresponds to the trigonometric function which they represent.
> **Note:** There can be some scenarios where we are not able to ensure whether the number is within domain of trigonometric function or not. Like π/2 is out of domain of tan(x), and if the interval generated by the input number is [π/2 - e, π/2 +e] then we can't out put any result because the result could be anything between -∞ to +∞, so we will iterate for more accurate input or more precise interval. We keep on iterating for more precise input until we don't have any such points(points which are out of domain) inside interval, and if we reach maximum precision and still don't get such intervals then we throw error saying number is not inside domain of trigonometric functions.
>
> Also, a lot of times when the interval generated by input is too large then sin and cos will give [-1, 1] as output as the input is bigger than interval or contains both 1 and -1.
## boost::real::const_precision_iterator interface

Expand Down Expand Up @@ -203,7 +206,7 @@ The boost::real::const_precision_iterator is a forward iterator [4] that iterate
## Examples

### Example 1
### Example 1: Declaring a real number and using precision iterator.
Defining simple real explicit numbers and creating an operation between them.
```cpp
#include <iostream>
Expand Down Expand Up @@ -258,7 +261,7 @@ d: [0.99999899999999999999999999999999999999999999999999999999999999999999999999
g: [0.9999979999999999999999999999999999999999999999999999999999999999999999999999999999999999991629249303, 0.99999799999999999999999999999999999999999999999999999999999999999999999999999999999999999965383428597]
h: [0.00000099999999999999999999999999999999999999999999999999999999999999999999999999999999999968217350127, 0.00000100000000000000000000000000000000000000000000000000000000000000000000000000000000000066399221262]
```
### Example 2
### Example 2: Integer and rational number.
Use of specialized data types for integer and rational types of numbers.
```cpp
#include <iostream>
Expand All @@ -284,7 +287,7 @@ int main(){
20/10 = 2
```

### Example 3
### Example 3: Power, exponent, and logarithm.
Use of power, exp and logarithm functions.
```cpp
#include <iostream>
Expand Down Expand Up @@ -362,7 +365,149 @@ c iteration [1.75601919365244082540382634326620308214439845791921676163955823715
c iteration [1.75601919365244082540382634326620308214439845791921676263247861654663653931, 1.75601919365244082540382634326620308214439845791921676263247861836978557027]
c iteration [1.75601919365244082540382634326620308214439845791921676263247861783286263563401048786, 1.75601919365244082540382634326620308214439845791921676263247861783286263733195034044]
```
### Example 4: Use of Irrationals, trigonometric functions, and controlling the precision
```cpp
#include <iostream>
#include <string>
#include <real/real.hpp>
#include <real/irrationals.hpp>
using real = boost::real::real<int>;
using type = boost::real::TYPE;
int main(){
// importing π from irrational numbers
real pi = boost::real::irrational::PI<int>;
int precision = pi.maximum_precision();
std::cout<<"Current(default) maximum precision of π is "<<precision<<std::endl;
std::cout<<"π = "<<pi<<std::endl;
//feeding π to sin funtions
real sin = real::sin(pi);
std::cout<<"maximum precision of sin is "<<sin.maximum_precision()<<std::endl;
std::cout<<"sin(π) = "<<sin<<std::endl;
// changing the maximum precision of π to 4
precision = 4;
std::cout<<std::endl<<"Changing the precision to "<<precision<<std::endl;
pi.set_maximum_precision(precision);
precision = pi.maximum_precision();
std::cout<<"Current maximum precision of π is "<<precision<<std::endl;
std::cout<<"π = "<<pi<<std::endl;
//feeding π to sin funtions
sin = real::sin(pi);
std::cout<<"maximum precision of sin is "<<sin.maximum_precision()<<std::endl;
std::cout<<"sin(π) = "<<sin<<std::endl;
// changing the maximum precision of π to 4
precision = 20;
std::cout<<std::endl<<"Changing the precision to "<<precision<<std::endl;
pi.set_maximum_precision(precision);
precision = pi.maximum_precision();
std::cout<<"Current maximum precision of π is "<<precision<<std::endl;
std::cout<<"π = "<<pi<<std::endl;
//feeding π to sin funtions
sin = real::sin(pi);
std::cout<<"Changing the max precision of sin(x) to "<<precision<<std::endl;
sin.set_maximum_precision(precision);
std::cout<<"maximum precision of sin is "<<sin.maximum_precision()<<std::endl;
for(auto itr = sin.get_real_itr().cbegin(); itr!= sin.get_real_itr().cend(); ++itr){
std::cout<<"iteration of sin "<<itr.get_interval()<<std::endl;
}
return 0;
}
```
### Outputs
```cpp
Current(default) maximum precision of π is 10
π = [3.14159265358979323846264338327950288419716939937510582097494459230781640628620899857018879027, 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899909729869626]
maximum precision of sin is 10
sin(π) = [-0.00000000000000000000000000000000000000000000000000000000000000000000000000000000046926387115300929617891203965075348871529147710776928643332557037631200996141466502084746304038157584795962599970645623, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000005784603607199635543420190606638535742513456551916863846852111123904494210854373938807153332655924202988609274069839854]

Changing the precision to 4
Current maximum precision of π is 4
π = [3.14159265358979323846264338327950288419716939937510582097494459230781640628620899857018879027, 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899909729869626]
maximum precision of sin is 10
sin(π) = [-0.00000000000000000000000000000000000000000000000000000000000000000000000000000000046926387115300929617891203965075348871529147710776928643332557037631200996141466502084746304038157584795962599970645623, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000005784603607199635543420190606638535742513456551916863846852111123904494210854373938807153332655924202988609274069839854]

Changing the precision to 20
Current maximum precision of π is 20
π = [3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193840814383439, 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193866690701871]
Changing the max precision of sin(x) to 20
maximum precision of sin is 20
iteration of sin [-0.75680249588262146557627106619511730962, 0.90929742704336957429169302728719559266]
iteration of sin [-0.00000000014219486076669568434490500064097155099775381723, 0.00000000172045029454540068127888392671949450435467244068]
iteration of sin [-0.00000000000000000066221795425345706087009609146842893796417261132994434994, 0.00000000000000000107250553141633901156507887913240873382892218791208238911]
iteration of sin [-0.00000000000000000000000000000056756336937149688420466722072244490816163882699034601216485282, 0.00000000000000000000000000161501958180750930312583102164678911769019841710356322345564170992]
iteration of sin [-0.00000000000000000000000000000000000058854302776876417518939224297209576957337161088963318285869547127370406908, 0.00000000000000000000000000000000000091608975459563626753582969546076157327382314364640215557540959899599515737]
iteration of sin [-0.00000000000000000000000000000000000000000000024743386142841868949561397383353141949892489631935611663024561552419153076620962238, 0.00000000000000000000000000000000000000000000115386461725163704869372745336921335446351376043050952525478534789606901810880312989]
iteration of sin [-0.00000000000000000000000000000000000000000000000000000007100454489577926339515850355825310338766061421904738079548985725713106977809533312832055726, 0.00000000000000000000000000000000000000000000000000000123405636511186519276144443461026225405272259485960328832307494329257515382487704517100636374]
iteration of sin [-0.00000000000000000000000000000000000000000000000000000000000000007149151092024619349524791498756507320469629980268919269968095045496574272522251931179584400173573108, 0.00000000000000000000000000000000000000000000000000000000000000114394117751603240483454361627111375693945277323385015244165583236406542518755436962100735363726922795]
iteration of sin [-0.00000000000000000000000000000000000000000000000000000000000000000000000033332398690835328897286489413492579843299880459270715057453700946046175219677339471656526126369258262545972412, 0.00000000000000000000000000000000000000000000000000000000000000000000000079863591586637405964381349929313857598739622279800813139950192007881952302730905543809773149295349412165664945]
iteration of sin [-0.00000000000000000000000000000000000000000000000000000000000000000000000000000000046926387115300929617891203965075348871529147710776928643332557037631200996141466502084746304038157584795962599970645623, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000058495594231518329661436202463278323737005398958721563550637575596551142226703333880794257506340355505957519368652000627]
iteration of sin [-0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000036410802923221941595054519935326786532344354189334002394437695262741427453258429102099166488938831195061842161343793885534999802, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000061771068257201715396777290419375967050373927313905117309493380968533555638368741127277199889678659752425639226348953117253417505]
iteration of sin [-0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000019577097066444506394897748006488851946672919325228302978179653876357403782564796161021242154289919910401623207312672350933401065051097033605979075, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000071861896237350500453514047835409612448848150815158939041147173225173437639854978106328359785326238795561263593785493470650768891092068209124586947]
iteration of sin [-0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000031463759314073327593223523953913353584282884421709297123238086177755867596032455618631711904259077939281071301576433779933402367414186272475294666, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000053695439508485906658217543574192445400095768057184039830666074141075542874845387933265844343975510305932770026907767457798709531204914638295301101]
iteration of sin [-0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012745506152883731973740707450957939039845965949983073588512705915429572575032762451065775784843659533345833190911742145159993057177896233203641670487491285, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000066565178331393137687759689998987217230693048635773988798006073276089680668650713835089377370526366849538512909724704995108312563125413750079480923697521495]
iteration of sin [-0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000015423816134811406050832809963134693770202892773799050904925628241987402831434949355521279420138641698876122364848958453835279955085224900272733124455912869000800391, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000058440014802392266354008143230367371581640227764092103274525874117814960062978387796412750863988008291052468707802246146679971081785677111846676854657744461611324377]
iteration of sin [-0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000081222184466415059677463867751792305244005011735281103469143336171081152059119643186703502456318381359782036707060092510165886642860224115795268220491082916214823225796283702, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000606688348808907733932050144393131535899426163003229504933953982834623038959385887844169864373412754558437906323560398327469221973462589858566725005540214167777383563338339998]
iteration of sin [-0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000188127220043804630550492864041695079714106615626912026450771527829289627143744423057497670352039506147521271218859760767210325373299697989653592559893340610778713476219122185912860491, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000452539389806276713150341001243077049034865943218614471548048939634018113862718925666289115024614661509666890467396794535918424483741660178167051620157346753805752149973788661408350085]
iteration of sin [-0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007535483041267511028940931335381616974162848323561947345986121474296768944793816895443085936266354852400199854281528803704397693336276475323926185565068796915230764261138608567372233300869874, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000589131794903704797589552070264673345577023613822437478886517722329431505976813852696372900906683843094070589893594767962426437549165184225785904718090387764306122010487795901067098337428936922]
iteration of sin [-0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003063284603671584306153467554388011766987308332932900670429691792834385895032200001900714667237445884611161759322381669350026435729817842698370796314911237821296059959356045454694708117571043904495156, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000525056859965331455751337219004506675858139888626997322818565138353638900638043697146523798430051736183591882457150738690706953849567966121144599179056529817104722958400043051314906525172419258744436285]
```
### Example 5: Some exception handling
```cpp
#include <iostream>
#include <string>
#include <real/real.hpp>
#include <real/irrationals.hpp>
using real = boost::real::real<int>;
using type = boost::real::TYPE;
int main(){
real x("-1");
// trying to find log of x and checking whether it throws error or not
try{
real y = real::log(x);
}catch(const boost::real::logarithm_not_defined_for_non_positive_number& e1){
std::cout<<"Caught log not defined for non positive number error."<<std::endl;
}
// out of domain number for trigonometric functions
x = real("0");
try{
real y = real::cosec(x);
}catch(const boost::real::max_precision_for_trigonometric_function_error& e1){
std::cout<<"Caught number out of domain of trigonometric functions error"<<std::endl;
}
// Non integral power of a negative number
x = real("-4");
real y("1.5");
try{
real z = real::power(x, y);
}catch(const boost::real::non_integral_power_of_negative_number& e1){
std::cout<<"Caught power of non integral power of negative number exception."<<std::endl;
}
// Square root of a negative number
try{
real z = real::sqrt(x);
}catch(const boost::real::sqrt_not_defined_for_negative_number& e1){
std::cout<<"Caught square root for negative number not supported exception."<<std::endl;
}
return 0;
}
```

### Output
```cpp
Caught log not defined for non positive number error.
Caught number out of domain of trigonometric functions error
Caught power of non integral power of negative number exception.
Caught square root for negative number not supported exception.
```
## References
1. Computable calculus / Oliver Aberth, San Diego : Academic Press, c2001
2. Lambov, B. (2007). RealLib: An efficient implementation of exact real arithmetic. Mathematical Structures in Computer Science, 17(1), 81-98.
Expand Down

0 comments on commit 548dec5

Please sign in to comment.