Skip to content

Instantly share code, notes, and snippets.

@joennlae
Created October 29, 2016 09:39
Show Gist options
  • Save joennlae/2b0cf0e35d6c3821aaad715e4eefbfa8 to your computer and use it in GitHub Desktop.
Save joennlae/2b0cf0e35d6c3821aaad715e4eefbfa8 to your computer and use it in GitHub Desktop.
// UNCOMMENT THE NEXT LINE TO ENABLE THE TESTS AND BEFORE SUBMITTING
//#include "tests.h"
#include <iostream>
using namespace std;
// PRE 0<= x < 2
// POST: return value is the binary representation of the inputed variable with 16 significant bits
void binExp(double dec);
int main(){
double d;
cin >> d;
if(d >= 0 && d < 2) binExp(d);
else cout << "not allowed";
return 0;
}
void binExp(double dec){
for(int i = 0; i < 16; i++){
if(dec>=1){
cout << 1;
dec -= 1;
dec *= 2;
}
else{
cout << 0;
dec *= 2;
}
if(i==0) cout << ".";
}
}
// UNCOMMENT THE NEXT LINE TO ENABLE THE TESTS AND BEFORE SUBMITTING
#include "tests.h"
#include <iostream>
#include <cmath>
using namespace std;
double calcFunc(double x){
return 0.9*x + 0.7;
}
double precision(double x){
//if(nextafter(x,0)>nexttoward(x,0)) return nextafter(x,0)-x; // too exact
//else return x-nexttoward(x,0); // ungefähre abschätzung http://de.cppreference.com/w/cpp/numeric/math/nextafter
return x * 1e-5; // to upper one is probably more exact than the one in the tests.h
}
void checkOnG(double a,double b, double p){
if( a - b < p) cout << "yes";
else cout << "no";
}
int main(){
double x,y,p;
cin >> x >> y;
p = precision(x);
if(y-calcFunc(x) > 0){ // y is bigger than the function
checkOnG(y,calcFunc(x),p);
}
else if(y-calcFunc(x) < 0){ // y is smaller than f | we need this because we are not allowed to use abs
checkOnG(calcFunc(x),y,p);
}
else{ // point is on g exactly, even in binary represention
cout << "yes";
}
return 0;
}
// UNCOMMENT THE NEXT LINE TO ENABLE THE TESTS AND BEFORE SUBMITTING
//#include "tests.h"
#include <iostream>
using namespace std;
// PRE: x is roundable to a number
// in the value range of type int
// POST: return value is the integer
// nearest to x, or the one closer
// to 0 if x lies right in between
// two integers.
int round(double x);
int main(){
double a;
cin >> a;
cout << round(a);
}
int round(double x){
double tmp;
tmp = x;
tmp += 0.5 - x;
if( x <= 0){ //0.0 - 0.5
return x;
}
else{ //0.51 - 0.999
return x + 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment