Skip to content

Instantly share code, notes, and snippets.

@fclairamb
Last active December 3, 2021 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fclairamb/0387016387c5474e06476f0a84b6ad86 to your computer and use it in GitHub Desktop.
Save fclairamb/0387016387c5474e06476f0a84b6ad86 to your computer and use it in GitHub Desktop.
round.cpp
#ifdef __SHELL__
set -ex
if [ "$0" = "sh" ]; then
curl https://gist.githubusercontent.com/fclairamb/0387016387c5474e06476f0a84b6ad86/raw/round.cpp > round.cpp && \
chmod a+rx round.cpp && \
./round.cpp
exit $?
fi
BIN=test-$(arch)
if [ "$FLAGS" = "" ]; then
FLAGS="-ffast-math -ffp-contract=fast -O3"
fi
g++ $FLAGS $0 -o $BIN
./$BIN
exit $?
#endif
#include <stdio.h>
void example_1() {
printf("Example 1:\n");
double x = 50178230318.0;
double y = 100000000000.0;
double ratio = x/y;
printf("x/y = %18.18f\n", ratio);
}
#include <cstdlib>
#include <iostream>
#include <cmath>
double round(double v, double digit)
{
double pow = std::pow(10.0, digit);
double t = v * pow;
//std::cout << "t:" << t << std::endl;
double r = std::floor(t + 0.5);
//std::cout << "r:" << r << std::endl;
return r / pow;
}
void example_2() {
std::cout << "Example 2:" << std::endl;
std::cout << round(4.45, 1) << std::endl;
std::cout << round(4.55, 1) << std::endl;
}
int main(int argc, char **argv) {
example_1();
example_2();
}
#!/usr/bin/env python3
from shapely.geometry import LineString
line1 = LineString([(3726.17, -181.848), (3726.19, -181.794)])
line2 = LineString([(3726.18, -181.729), (3726.25, -182.435)])
point = line1.intersection(line2)
print(point, point.is_valid)
@fclairamb
Copy link
Author

fclairamb commented Sep 9, 2021

Run it with:

curl https://gist.githubusercontent.com/fclairamb/0387016387c5474e06476f0a84b6ad86/raw/round.cpp | sh
curl https://gist.githubusercontent.com/fclairamb/0387016387c5474e06476f0a84b6ad86/raw/round.py | python3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment