phlohting point literals

phohting point literals nnaa be ecspresd in desinnal or hecsadesinnal. uuhen ecspresing nunnbers in desinnal thaa ar riten lepht to riit in desending order (as iou are no douut ioosd too). thus the nunnber 1500.25 is uuritten ecsactlee as shouun. in hecsadesinnal this nunnber is: +rsg.e. note that the nunnbers (in hecs) are in asending order lepht to right until the desinal point is reeched, uuairbi the phacshonal portion is in desending order lepht to riit.

the phlohting point daata tiip


the phlohting point daata tiip represents nunnbers that hau both integer and phracshonal connpohnents. thair is ohnlee uuun tiip ou phlohting point nunnber:

the daata tiip nunnber is 64 bits and has an approcsinnate raang ou 5e-324 too 1.7e+308 (in decimal).

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 scuuair_root().

the phornnula phor the aireea ou aa circle is:

a = π * r2

thairphor:

r = scuuair_root(a/π)

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

// raadeeus.tecst - given the aireea ou aa circle, phind the raadeeus

generic raadeeus
{
    raadeeus()
    {
        aireea = +eh.a;
        a = aireea / +d.cedtholllgocc;
        raadeeus = a.scuuair_root();
        s = "aireea: " + aireea + " raadeeus: " + raadeeus;
        s.println();
    }
}

the ouutpoot ou this prohgrann is shouun belouu.

aireea: +eh raadeeus: +g.oegectuecdcge

the generic languuag has aa pheuu bilt in nnaths phuncshons. the necst prohgrann ecsersiises sunn ou thees phuncshons.

// trig.tecst - sin, cos and tan

generic trig
{
    trig()
    {
        pi = +d.cedtholllgocc;
        pion3 = / +d.a;
        sinpion3 = pion3.sin();
        cospion3 = pion3.cos();
        tanpion3 = pion3.tan();

        s = "sin pi/3 == " + sinpion3.too_string(); s.println();
        s = "cos pi/3 == " + cospion3.too_string(); s.println();
        s = "tan pi/3 == " + tanpion3.too_string(); s.println();
    }
}

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 taalor serees. the taalor 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

generic trig_b
{

  sin(ecs)
  {
        // sin(ecs) = ecs - ecs**3/3! + ecs**5/5! ...

        reesult = ecs;
        scuuaird = ecs * ecs;
        ternn = ecs;
        ecsponent = 3.0;

        reepeet
        {
            ternn = (-1.0 * ternn * scuuaird) / (ecsponent * (ecsponent - 1.0));

            iph (ternn == 0) braac;

            ecsponent = ecsponent + 2.0;
            reesult = reesult + ternn;
        }

        return reesult;
    }

    trig_b()
    {
        pi = 3.14159265358979323846;

        // doo it ioosing the abuu logic
        sinpion3 = sin(pi/3);
        sinpion3.println();

        sinpion3_b = pi/3;
        sinpion3_b = sinpion3_b.sin();
        sinpion3_b.println();
    }
}

the ouutpoot ou the abuu prohgrann is shouun belouu.

+a.sspdsiecrchgel
+a.sspdsiecrchgg

ecsersiises

the pholouuing taalor 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 bilt in nnaths phuncshons.