It's UWAweek 47

help2002

This forum is provided to promote discussion amongst students enrolled in CITS2002 Systems Programming.
Please consider offering answers and suggestions to help other students! And if you fix a problem by following a suggestion here, it would be great if other interested students could see a short "Great, fixed it!"  followup message.
Displaying selected article
Showing 1 of 828 articles.
Currently 87 other people reading this forum.


 UWA week 36 (2nd semester, mid-semester break) ↓
SVG not supported

Login to reply

👍?
helpful
10:34am Thu 5th Sep, Chenjun H.

How exactly can we ensure that integers are printed in that way without using 2 separate data types? Isn't it possible for an integer stored as float to have imprecision?

Furthermore, since the arguments passed to print could be an expression, wouldn't doing this using an if condition and nearbyint() in the c code generated require dynamic memory allocation?

for example, a print statement can be as follows, since example 5 allows for addition in print statements.

print count + count * count - count * count * x

A c representation that properly checks if this is an integer or float would need to generate code that looks like

if (nearbyint(count + count * count - count * count * x) == count + count * count - count * count * x) {
    print("%.0lf\n", count + count * count - count * count * x);
} else {
    print("%.6lf\n", count + count * count - count * count * x);
}

to check whether the result of the expression is an integer

We would need to store the entire expression of count + count * count - count * count as a string and substitute it into the code generated, which can only be done with dynamic memory allocation, since the expression isn't guaranteed to have a maximum length.

The University of Western Australia

Computer Science and Software Engineering

CRICOS Code: 00126G
Written by [email protected]
Powered by history
Feedback always welcome - it makes our software better!
Last modified  8:08AM Aug 25 2024
Privacy policy