It's UWAweek 42 (2nd semester, week 12)

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 the 5 articles in this topic
Showing 5 of 674 articles.
Currently 3 other people reading this forum.


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

Login to reply

👍?
helpful
5:03pm Wed 4th Sep, Chenjun H.

Hi, in the examples, the print statement has inconsistent behavior, for example in sample 4, when 2 integers are multiplied, the printed value is 24, but in example 2, the printed value for a decimal/float value is 2.500000 with no truncation. Does this imply that our program should store 2 types, one for float and another for decimal, and disambiguate them when printing?


SVG not supported

Login to reply

👍?
helpful
7:25pm Wed 4th Sep, Joshua N.

Hi Chenjun,

When printed, numbers that are exact integers must be printed without any decimal places; other numbers must be printed with exactly 6 decimal places.

Your program wouldn't need two datatypes, it would just need to change the format string of printf.


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.


SVG not supported

Login to reply

👍?
helpful
12:10pm Thu 5th Sep, Joshua N.

"Chenjun Hou" <23*6*8*9@s*u*e*t*u*a*e*u*a*> wrote:
> 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?
There are no integers in the ml program, all the constants are real numbers, as that is the only datatype supported by ml. So, all the constant integer values in the ml program are actually floats. e.g. "2" is technically "2.0". If there are rounding errors in your program there will be in the program used to mark it, since it would also be using floating point values for everything.
> > ``` > 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
This is one way you could do it. Or you could evaluate the result of the expression and place it in the c code since you will likely need to check the identifiers in the string anyway.
> 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.
Technically you don't need dynamic memory allocation, you could have a large array, copy the string into the array, and as long as the string is terminated by a '\0' then any string operations will stop at that point. Here is some code for you to play with:
#include <stdio.h>
#include <string.h>

int main() {
    char buffer[1000]; //note the excessively large buffer
    char line[10000]; // note the even larger buffer
    float pi = 3.14159;

    sprintf(buffer, "The value of pi is %.2f.", pi);
    sprintf(line, "%s %s", buffer, buffer);
    printf("%s\nlength of line is %i\n", line, strlen(line));
    
    
    //floats
    float num = 20.5;
    float num2 = 1.12321;
    float num3 = 3;
    
    if(num == (int) num)
    {
        printf("int\n");
    } else {
        printf("float\n");
    }
    
    printf("%.7f\n", num2 + num3);
    printf("%.7f\n", num * num2);
    printf("%.7f\n", num + num2);
    printf("%.7f\n", num3 * num3);
    
    return 0;
}


SVG not supported

Login to reply

👍?
helpful
4:50pm Fri 6th Sep, Joshua N.

Actually sorry, I just realized made a mistake the datatype used should be doubles not floats because it has more precision (e.g. reduces rounding errors). So that way if you're multiplying/dividing real numbers and showing 6 decimal places the errors shouldn't (usually) show.

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