Difference between revisions of "Closest To Origin Assignment"

From CSE425S Wiki
Jump to navigation Jump to search
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
=Background=
 +
The distance to the origin for a point (x, y) is:
 +
 +
<math>\sqrt {x^2 + y^2}</math>
 +
 +
Is the sqrt required when comparing two points to see which is closest to the origin or can you just determine which point's squared distance is closer?
 +
 +
=Basis Library=
 +
==[https://smlfamily.github.io/Basis/option.html Option]==
 +
*[https://smlfamily.github.io/Basis/option.html#SIG:OPTION.option:TY NONE]
 +
*[https://smlfamily.github.io/Basis/option.html#SIG:OPTION.option:TY SOME(v)]
 +
 
=Code to Implement=
 
=Code to Implement=
  
Line 6: Line 18:
 
Write a function
 
Write a function
  
fun closest_to_origin(xys : (real*real) list) : (real*real) option =
+
<syntaxhighlight lang="sml">
 +
fun closest_to_origin(xys : (real*real) list) : (real*real) option =
 +
</syntaxhighlight>
  
 
which returns closest point to the origin, if it exists.
 
which returns closest point to the origin, if it exists.
  
 
=Test=
 
=Test=
{{SMLUnitTesting|closest_to_origin|closest_to_origin}}
+
{{SMLUnitTesting|run_closest_to_origin_testing|closest_to_origin}}

Latest revision as of 18:48, 10 July 2023

Background

The distance to the origin for a point (x, y) is:

Is the sqrt required when comparing two points to see which is closest to the origin or can you just determine which point's squared distance is closer?

Basis Library

Option

Code to Implement

file: src/main/sml/closest_to_origin/closest_to_origin.sml Smlnj-logo.png
functions: closest_to_origin

closest_to_origin

Write a function

fun closest_to_origin(xys : (real*real) list) : (real*real) option =

which returns closest point to the origin, if it exists.

Test

source folder: src/test/sml/closest_to_origin
how to run with CM.make verbosity off: sml -Ccm.verbose=false run_closest_to_origin_testing.sml
how to run with CM.make verbosity on: sml run_closest_to_origin_testing.sml

note: ensure that you have removed all printing to receive credit for any assignment.

SML Error Messages