3

Is this even possible? i tried to google it but i cant seem to find the right answer.

I need to limit the decimal places since the answer will really differ if there is only 3 decimal places than 5, so i was hoping that you could help me with these. i know how to print with 3 decimal places but to declare a variable to only hold 3 decimal places is something i do not know. i could also use some links if you have any.

float sinfa(float num1)
{
    float fc;
    float powers;
    float rad_angle;

    rad_angle = num1 * (PI / 180.0);
    powers = pow(num1,4);
    fc = sin(rad_angle)-powers+1;
    return (fc);
}

float sinfb(float num2)
{
    float fd;
    float powerss;
    float rad_angle1;

    rad_angle1 = num2 * (PI / 180.0);
    powerss = pow(num2,4);
    fd = sin(rad_angle1)-powerss+1;
    return (fd);
}

float tp(float fa,float fb,float num1,float num2)
{
    float p;
    float fm2 = fa*num2;
    float fm1 = fb*num1;
    p = (fm2-fm1)/(fa-fb);
    return (p);
}

float sinp(float p1)
{
    float fop;
    float ppowers;
    float rad_angle2;

    rad_angle2 = p1 * (PI / 180.0);
    ppowers = pow(p1,4);
    fop = sin(rad_angle2)-ppowers+1;
    return (fop);
}

Thank you

magicianiam
  • 1,352
  • 6
  • 31
  • 64
  • Just write code that does exactly what you want. For example, you could hold the integral part in one variable and the thousandths in another. Or store the value times 1,000 as an integer. – David Schwartz Feb 01 '13 at 02:46
  • It sounds like you're trying to solve the wrong problem. Can you explain what you're trying to do? – John Carter Feb 01 '13 at 02:47
  • lets say i want to multiple 1.2345 with 3.1234 but i want it to be 1.234 with 3.123 since the 4th decimal can have a big difference to the answer for the multiplication. – magicianiam Feb 01 '13 at 02:48
  • If the value is 3.254, store it in an integer as 3,254. That way, it can only hold three decimal places. If you use an integer to store thousandths, then it is storing only three decimal places. (Just be careful when you do operations. For example, when you multiply you need to divide by 1,000.) But the point is -- code what you need. If you need X, code X. – David Schwartz Feb 01 '13 at 02:51
  • but integers cant hold decimal values? right? – magicianiam Feb 01 '13 at 02:52
  • You *want* integer thousandths. That's what "only contain 3 decimal places" means. To store the *value* 5.231, store 5,231 in an integer. (Write whatever assignment, display, and operation functions that you need.) – David Schwartz Feb 01 '13 at 02:52
  • 2
    @magicianIam David Schwartz is telling you to do this: `intVal = 1.234567 * 1000`. The value of intVal is now the integer 1234. When you need to convert it back to a decimal with three decimal places, do this: `floatVal = intVal / 1000`. The value of floatVal is now the decimal 1.234. – Nocturno Feb 01 '13 at 03:13
  • so it would be int = varsomething. varsomething = 1.234567 * 1000; ? – magicianiam Feb 01 '13 at 03:15

2 Answers2

2

Can't be done. A float is a 32-bit floating-point value in every C compiler I have ever used. There is nothing standard in the language to redefine how it works.

You could write a set of fixed-point functions that store values multiplied by 1000, or use a general library that implements fixed-point with arbitrary precision and set the precision to 3 decimal digits.

C++ fixed point library?

Community
  • 1
  • 1
steveha
  • 71,436
  • 20
  • 89
  • 116
  • 1
    "A `float` is a 32-bit integer." – us2012 Feb 01 '13 at 03:07
  • i was hoping there would be a simplier method for this, never did i once tried to use a 3 decimal point float until now. – magicianiam Feb 01 '13 at 03:10
  • @us2012, that was the mental equivalent of a typo. I meant to say "floating point value" and I don't know how I could have typed "integer" there. Thanks for pointing it out; I edited the answer to correct it. And I guess theoretically a `float` might be some other size than 32-bits, like maybe on some weird minicomputer it would be 40 bits or something, so I added some qualifying text on the 32-bit claim just to be safe! – steveha Feb 01 '13 at 03:49
  • theoretically a float is stored as int anyway(bytewise) and interpreted by the FPU instead of the CPU. So technically, to the computer, its just byte-array. – Aniket Inge Feb 01 '13 at 03:51
2

It can be done!

int intVal = 1.234567 * 1000;          // result: 1234
float floatVal = (float)intVal / 1000; // result: 1.234

The trick is to:

  1. Move the required number of digits to the left-hand side of the decimal place.
  2. Truncate all the digits on the right-hand side of the decimal place.
  3. Move the required number of digits back to the right-hand side of the decimal place.
Nocturno
  • 8,904
  • 5
  • 30
  • 39