Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
GoikLectures
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Goik Martin
GoikLectures
Commits
5cf12a9f
Commit
5cf12a9f
authored
4 years ago
by
Goik Martin
Browse files
Options
Downloads
Patches
Plain Diff
Cosmetics
parent
7f9611b7
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Doc/Sd1/Exam/2020/Summer/exam.xml
+55
-19
55 additions, 19 deletions
Doc/Sd1/Exam/2020/Summer/exam.xml
with
55 additions
and
19 deletions
Doc/Sd1/Exam/2020/Summer/exam.xml
+
55
−
19
View file @
5cf12a9f
...
...
@@ -190,8 +190,9 @@ Provided: double</screen>
</listitem>
<listitem>
<para>
The cleanest way is replacing the
<code>
double
</code>
literal by a a
<code>
float
</code>
literal:
</para>
<para>
Replacing the
<code>
double
</code>
literal by a a
<code>
float
</code>
literal leaves us with an overall
<code>
float
</code>
expression resolving the issue:
</para>
<screen>
radius * radius * 3.1415926
<emphasis
role=
"red"
>
F
</emphasis>
...
...
@@ -200,8 +201,6 @@ Provided: double</screen>
int * float
⭨ ⭩
float
</screen>
<para>
This resolves the return type clash.
</para>
</listitem>
</orderedlist>
</answer>
...
...
@@ -229,14 +228,14 @@ Provided: double</screen>
adds a logging statement:
</para>
<programlisting
language=
"java"
>
static public int getSquare (int value) {
int result = value * value;
final
int result = value * value;
log.info(result);
return result;
}
</programlisting>
<para>
The team is baffled: Albeit squares of integer values are
expected to be
always
positive occasionally negative results
show
up:
</para>
expected to be
strictly
positive occasionally negative results
show
up:
</para>
<screen>
INFO [main] sd1.SupportMethods (SupportMethods.java:15) - -1757895751
</screen>
...
...
@@ -289,9 +288,23 @@ Provided: double</screen>
</informaltable>
<para>
The product
<code>
46341 * 46341
</code>
of type
<code>
int
</code>
however exceeds this value for the very first
time. Due to an
<code>
int
</code>
's four-byte two complement
representation we have:
</para>
<code>
int
</code>
for example exceeds
<inlineequation>
<m:math
display=
"inline"
>
<m:mrow>
<m:msup>
<m:mi>
2
</m:mi>
<m:mi>
31
</m:mi>
</m:msup>
<m:mo>
-
</m:mo>
<m:mi>
1
</m:mi>
</m:mrow>
</m:math>
</inlineequation>
for the very first time. Due to an
<code>
int
</code>
's four-byte two complement representation we
have:
</para>
<informaltable
border=
"1"
>
<tr>
...
...
@@ -306,12 +319,16 @@ Provided: double</screen>
<td
valign=
"top"
><screen>
-2147479015
</screen></td>
</tr>
</informaltable>
<para>
Starting from
<code>
46341
</code>
all computed squares
are simply wrong due to arithmetic overflow regardless of
their result's sign.
</para>
</listitem>
<listitem>
<para>
Solving the issue requires a data type
being able to
accommodate
squares of arbitrary
<code>
int
</code>
values. The
smallest int value
is
<inlineequation>
<para>
Solving the issue requires a data type
to accommodate
squares of arbitrary
<code>
int
</code>
values. The
smallest int
value (having maximum amount)
is
<inlineequation>
<m:math
display=
"inline"
>
<m:mrow>
<m:mo>
-
</m:mo>
...
...
@@ -323,7 +340,7 @@ Provided: double</screen>
</m:msup>
</m:mrow>
</m:math>
</inlineequation>
and i
ts square thus
<inlineequation>
</inlineequation>
. I
ts square
is
thus
<inlineequation>
<m:math
display=
"inline"
>
<m:msup>
<m:mi>
2
</m:mi>
...
...
@@ -331,12 +348,31 @@ Provided: double</screen>
<m:mi>
62
</m:mi>
</m:msup>
</m:math>
</inlineequation>
.We choose
<code>
long
</code>
as result
type. Turning an
<code>
int
</code>
into a long is easy:
</para>
</listitem>
</inlineequation>
. Choosing return type
<code>
long
</code>
allows for a maximum value of
<inlineequation>
<m:math
display=
"inline"
>
<m:mrow>
<m:msup>
<m:mi>
2
</m:mi>
<listitem>
<para/>
<m:mi>
63
</m:mi>
</m:msup>
<m:mo>
-
</m:mo>
<m:mi>
1
</m:mi>
</m:mrow>
</m:math>
</inlineequation>
which is ways larger than required. Turning
the
<code>
int
</code>
into a
<code
language=
"java"
>
long
</code>
expression is easy:
</para>
<programlisting
language=
"java"
>
static public long getSquare (int value) {
long result = value;
result *= value;
log.info(result);
return result;
}
</programlisting>
</listitem>
</orderedlist>
</answer>
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment