#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# beginner question: related to floats and constants

Member Posts: 1
I'm rather new : the message board section should probably explain that to anyone reading. I don't have a lot of experience with computers, matter of fact, I'm buying my first computer some time soon (currently using one that belongs to another family member). But in any case.

I'm testing a few things in C, in a compiler called "Miracle C", and there seems to be a problem with something I've done in an experimental program. I don't know what, else I'd fix it myself and go on my way, so I figured I'd post the short script here and see if anyone can help me pick up the error.

Also, I'm not entirely sure what the definite use of "short", "long", and "int", are. All numerical values, obviously, but why use one over the other..?

Anyways, here is the script, as it were.

/* stocks : testing constant, and floating */
#include

void main()
{
const float Revenue_per_150 = 4.5f;
short JanSold = 23500;
short FebSold = 19300;
short MarSold = 21600;
float RevQuarter = 0.0f;

short QuarterSold = JanSold + FebSold + MarSold;

/* output monthly sales and total for quarter */
printf("
Stock sold in
Jan: %d
Feb: %d
Mar: %d
",
JanSold, FebSold, MarSold);
printf("
Total stock sold in first quarter: %d",
QuarterSold);

/* calc. total rev. for quarter & output it */
RevQuarter = QuarterSold/150*Revenue_Per_150;
printf("
Sales revenue this quarter is: \$%.2f
", RevQuarter);
}

• Member Posts: 32
: I'm rather new : the message board section should probably explain that to anyone reading. I don't have a lot of experience with computers, matter of fact, I'm buying my first computer some time soon (currently using one that belongs to another family member). But in any case.
:
: I'm testing a few things in C, in a compiler called "Miracle C", and there seems to be a problem with something I've done in an experimental program. I don't know what, else I'd fix it myself and go on my way, so I figured I'd post the short script here and see if anyone can help me pick up the error.
:
: Also, I'm not entirely sure what the definite use of "short", "long", and "int", are. All numerical values, obviously, but why use one over the other..?
:
: Anyways, here is the script, as it were.
:
: /* stocks : testing constant, and floating */
: #include
:
: void main()
: {
: const float Revenue_per_150 = 4.5f;
: short JanSold = 23500;
: short FebSold = 19300;
: short MarSold = 21600;
: float RevQuarter = 0.0f;
:
: short QuarterSold = JanSold + FebSold + MarSold;
:
: /* output monthly sales and total for quarter */
: printf("
Stock sold in
Jan: %d
Feb: %d
Mar: %d
",
: JanSold, FebSold, MarSold);
: printf("
Total stock sold in first quarter: %d",
: QuarterSold);
:
: /* calc. total rev. for quarter & output it */
: RevQuarter = QuarterSold/150*Revenue_Per_150;
: printf("
Sales revenue this quarter is: \$%.2f
", RevQuarter);
: }
:

/* stocks : testing constant, and floating */
#include
#include /* where the system pause function is */

void main()
{
const float Revenue_per_150 = 4.5; /* you don't need this 'f' at the end */

/* There are two types of integers in C, short (2 byets) and long or int (4 bytes).
the max value of combinations in 2 bytes (8+8 bits) is 2^16, that is, 65536 possible numbers.
If such a number is declared as "unsigned short" (w/o a negative sign), then we can have numbers 0 to 65535.
If it is declared as signed (just short), then we have to split the number of combinations so we can have equal number of negative values, 0, and the positive values. Thus, our values now range from -32768 to 32767.
Remember, computer doesn't know what negative numbers are since they don't exist in the binary or in the hexadecimal system, and we have to trick it.
The same applies to the long integer of 4 bytes (32 bits), just the range is about 4 billion values for the unsigned, and -2 to 2 billion for the signed.
For a short program like this you can simply use int, but for a program that takes care of e.g. 1000 records, the difference btw 2 and 4 bytes should be observed.
*/

unsigned short JanSold = 23500;
unsigned short FebSold = 19300;
unsigned short MarSold = 21600;
float RevQuarter = 0.0;

/* the problem you had below is that you declared a signed short (max 32767 positive) and assigned to it the values above that crossed the bound of 32767 and the compiler simply resumed onto -32768 and further adding on to give you the negative result.
You can use the int type (the long) which gives you enough positive values, about 2 billion, as we said.
*/

int QuarterSold = JanSold + FebSold + MarSold;

/* Let's check the number of bytes in each of the two types: */
printf("Sizes of u_short and u_int: %d, %d
", sizeof(JanSold), sizeof(QuarterSold));

/* output monthly sales and total for quarter */
printf("
Stock sold in
Jan: %d
Feb: %d
Mar: %d
",
JanSold, FebSold, MarSold);
printf("
Total stock sold in first quarter: %d",
QuarterSold);

/* calc. total rev. for quarter & output it */
RevQuarter = QuarterSold/150*Revenue_per_150;
/* you mistyped the constant Revenue_P... c is case sensitive */

printf("
Sales revenue this quarter is: \$%.2f
", RevQuarter);

system("PAUSE");
}

I hope this helps you.
fuel730