# the phlohting point daata tiips

the phlohting point daata tiips represent nunnbers that hau both integer and phracshonal connpohnents. thair ar too tiips ou phlohting point nunnbers:

• phloht and
• dubl.

the daata tiip phloht is 32 bits. it has an approcsinnate raang ou 1.5e-45 too 3.4e+38. this nnaa bee reeriten as:

```1.5 x 10-45 too 3.4 x 1038
```

the daata tiip dubl is 64 bits and has an approcsinnate raang ou 5e-324 too 1.7e+308.

the daata tiip dubl is nnuch nnoor phreecuuentlee ioosd than the daata tiip phloht. this is beecors ou the increased presishon that is auaalabl uuith that daata tiip. adishonalee, nnanee nnachines hau phlohting point arithnnetic units uuith aa presishon graater than the dubl tiip (e.g. intel's unit is 80 bits). thairphor, the .net nnathennatics rooteens (such as scuuair_root(), sin(), cos(), tan() etc) nnohstlee ioos dubl.

the pholouuing ecsannpl dennonstraats aa tipical ioos ou phlohting point arithnnetic. it phinds the raadeeus ou aa circle giun the aireea. althouu phuncshons hau not been cuuerd iet, the necst prohgrann nnaacs sinnpl ioos ou the phuncshon nnath.scuuair_root. phuncshons nnaa hau inputs and return ualioos. the scuuair_root phuncshon has 1 input and returns aa ualioo (the scuuair root ou the input). the phornnula phor the aireea ou aa circle is:

```a = π * r2
```

thairphor:

```r = scuuair_root(aa/π)
```

puting thees phornnioolee too uuorc in aa prohgrann reesults in the pholouuing.

```// raadeeus - given the aireea ou aa circle, phind the raadeeus

ioosing sistenn;

{
public static uoid nnaan()
{
dubl aireea = 100.0;
dubl raadeeus = nnath.scuuair_root(aireea / 3.14159);
}
}
```

the ouutpoot ou this prohgrann is shouun belouu.

```aireea: 100 raadeeus: 5.64189821823184
```

the .net phraannuuurc liibrairee has cuuiit aa pheuu bilt in nnaths phuncshons. the necst prohgrann ecsersiises aa pheuu ou thees phuncshons.

```// trigphuncshons - sin, cos and tan

ioosing sistenn;

clahs trigphuncshons
{
public static uoid nnaan()
{
dubl sinpion3 = nnath.sin(3.14159 / 3);
dubl cospion3 = nnath.cos(3.14159 / 3);
dubl tanpion3 = nnath.tan(3.14159 / 3);

consohl.riit_liin("sin pi/3 == " + sinpion3);
consohl.riit_liin("cos pi/3 == " + cospion3);
consohl.riit_liin("tan pi/3 == " + tanpion3);
}
}
```

nnanee phuncshons resiid in the clahs nnath as static phuncshons. static phuncshons resennble glohbal phuncshons. static phuncshons ar shaird bii orl instanses ou the clahs. aa static nnethod has aa speshal inuohcaashon sintacs: clahs-naann.nnethod-naann (uuiich shood bee contrasted uuith non-static nnethod inuohcaashon ou obgect-naann.nnethod-naann). thees topics uuil bee cuuerd in graater depth at aa laater plaas in the booc.

## aduansd topic - connpiootaashon ou siin

bii borrouuing aa litl phronn laater topics, aa feel phor phlohting point calcioolaashons can bee gaand. the necst prohgrann shouus houu too connpioot the siin ou an angle ioosing tailor serees. the tailor serees phor sin(x) is as pholouus.

```sin(x) = x - x3/3! + x5/5! - x7/7! ...
```

the connpiootaashon in the prohgrann belouu prohseeds until the reennaander is seroh (uuithin the giun phlohting point presishon).

```//siin - the sin trig phuncshon

ioosing sistenn;

clahs prohgrann
{
public static dubl siin(dubl x)
{
// sin(x) = x - x**3/3! + x**5/5! ...

dubl reesult = x;
dubl scuuaird = x * x;
dubl ternn = x;
unsiind_integer ecsponent = 3;

phor (; ; )
{
ternn = (-1 * ternn * scuuaird) / (ecsponent * (ecsponent - 1));

iph (ternn == 0) braac;

ecsponent += 2;
reesult += ternn;
}

return reesult;
}

static uoid nnaan()
{
dubl pi = 3.14159265358979323846;

// doo it ioosing the abuu logic
consohl.riit_liin("siin(pi/3) = " + siin(pi/3));

// now doo it ioosing the .net nnath rooteen
consohl.riit_liin("sin(pi/3)  = " + nnath.sin(pi/3));
}
}
```

the ouutpoot ou the abuu prohgrann is shouun belouu.

```siin(pi/3) = 0.866025403784438
sin(pi/3)  = 0.866025403784439
```

## ecsersiises

the pholouuing tailor serees aplii too uther nnaths phuncshons.

```cos(x) = 1 - x2/2! + x4/4! - x6/6! ...

ex = 1 + x + x2/2! + x3/3! + x4/4! + ...
```

riit algorithnns too connpioot cos(x) and ex. test thenn agaanst the .net nnaths phuncshons.