Print the three results, each on a separate line, formatted with two decimal places, in the following order: Euclidean, City‑block, Chessboard.
📌 Important:
Use the standard mathematical functions of your language: math.sqrt, abs (or fabs), and max.
The output must contain only the numbers (one per line), without additional text.
See an interactive simulator for this problem at the Figure 1.11 (graph with draggable points and visualization of the three metrics).
1.19.4.1 🖼️ Why does this matter? – Computational cost
In a 1000×1000 pixel image (1 million pixels), computing the distance from each pixel to a reference point requires 1 million operations. The choice of metric affects performance:
The sqrt function is computationally more expensive than operations such as addition, subtraction, multiplication, and absolute value. On modern CPUs, the difference can be small (about 1.5× to 3×), but in embedded systems or in loops with millions of iterations, any gain matters. Therefore, when the goal is only to compare distances (e.g., finding the nearest point), use the squared Euclidean distance.
1.19.4.2 📋 Task (specification for VPL)
Input:
A single line with four real numbers: Ax Ay Bx By
Output:
Three lines, each with a real number with two decimal places (Euclidean, City‑block, Chessboard).
1.19.4.3 📌 Examples
Input
Output
Observation
0 0 3 4
5.00 7.00 4.00
3‑4‑5 triangle
0 0 1 1
1.41 2.00 1.00
Unit diagonal
Example of testing sqrt in Python, with timeit isolating each operation:
%%writefile tmp/mm_out_1.cpp// Compile: g++-O2 -std=c++17 benchmark.cpp -o benchmark && ./benchmark#include <iostream>#include <iomanip>#include <cmath>#include <chrono>const int N =50'000'000;double apenas_soma() { double a =3.0, b =4.0;return a + b;}double soma_e_sqrt() { double a =3.0, b =4.0;return std::sqrt(a*a + b*b);}int main() { auto start_soma = std::chrono::high_resolution_clock::now(); volatile double sum;for (int i =0; i < N;++i) {sum= apenas_soma(); } auto end_soma = std::chrono::high_resolution_clock::now(); std::chrono::duration<double> t_soma = end_soma - start_soma; auto start_sqrt = std::chrono::high_resolution_clock::now();for (int i =0; i < N;++i) {sum= soma_e_sqrt(); } auto end_sqrt = std::chrono::high_resolution_clock::now(); std::chrono::duration<double> t_sqrt = end_sqrt - start_sqrt;// Prevent optimization removalif (sum==12345.6789) std::cout <<sum; std::cout << std::fixed << std::setprecision(3); std::cout <<"Soma simples : "<< t_soma.count() <<" s\n"; std::cout <<"Soma + sqrt : "<< t_sqrt.count() <<" s\n"; std::cout <<"Razão (sqrt/soma) : "<< std::setprecision(2)<< (t_sqrt.count()/t_soma.count()) <<"x\n";return0;}