Floating point number precision related to calculation - c++11

#include
int main()
{
using namespace std;
double d1(0.1);
double d2(0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1);
cout << std::setprecision(17);
cout << d1 << endl; // 0.10000000000000001
cout << d2 << endl; // 0.99999999999999989
return 0
}
I understood there should be error value in floating number because of converting binary number to decimal number.
Therefore, I expected d2's value is more than 1.0.
Because d1 is more than 0.1 and d2 is 10 times of d1.
However, result is different. I don't know why d2 is less than 1.0.

Related

How to calculate variance with C++?

This is what I have so far. I am not able to get the right variance.
variance += ((numbers[i] - (double)mean) * (numbers[i] - (double)mean));
variance /= (double)size;
cout << "The variance is: " << variance << endl;
There are multiple problems with your code, all of which contribute to the variance being wrong.
You are calculating the variance in the loop, while updating the mean every iteration (before you have the correct mean). While this will result in the correct mean being stored in the mean variable at the end of the loop, the variance will have been computed with an incorrect mean in all but the last iteration.
You need the mean of all the numbers before you start subtracting it from the array to compute the variance. The solution would be another loop to calculate the variance after the original loop, at which point you already have the correct mean.
Array indices start at 0, but you start at 1, which means the fist number in your array will always be 0, and you are calculating mean and variance of 9 numbers instead of 10, but dividing by 10.
Mean is an integer, but is meant to store a floating point (or double precision) number. Additionally, total / size will round down since they are both integers, so you should cast one of them to double.
Put
variance += ((numbers[i] - (double)mean) * (numbers[i] - (double)mean));
outside the first for loop in another for loop.
And the code will become like this-
#include <iostream>
using namespace std;
int main()
{
int arraySize = 10;
int numbers[10];
int input, total = 0, size = 0, mean = 0;
double variance = 0;
for (int i = 1; i < arraySize; i++) {
cout << "Enter number " << i << " (-1 to end): ";
cin >> input;
if (input == -1) {
break;
}
else {
numbers[i] = input;
}
total += numbers[i];
size++;
mean = total / size;
}
for(int i=1; i<=size;i++)
variance += ((numbers[i] - (double)mean) * (numbers[i] - (double)mean));
variance /= (double)size;
cout << "The mean is: " << mean << endl;
cout << "The variance is: " << variance << endl;
}
While calculating variance you need the mean of all the numbers in array not the mean after each input.

Bisection method how to find x1 and x2?

I want to ask someone for help, I have to get x1 and x2 from, that function c *(x * x) + 2 * x - d;, where c is equal to random number from 2.00 to 8.99 and d is number between 5.00 and 14.99, from user input. My question is how to get those two variables? x1 and x2 ? I wrote code that can found only x1, how to get x2? x1 and x2 are places where function is equal to 0.
Thanks :).
My code:
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <random>
using namespace std;
double x1, x2, c, d, eps = 0.0001, b = 10, a=0;
int attempts=20;
double f(double x){
return c *(x * x) + 2 * x - d;
}
int main() {
mt19937 rnd(time(NULL));
uniform_real_distribution <double> random(2.00, 8.99);
c = random(rnd);
cout << "Random c is equal to : " << c << endl;
cout << "Enter d [real number from 5.00 -> 14.99]: ";
cin >> d;
while (d > 14.99 || d < 5.00) {
cout << attempts << " Attempts left: ";
cin >> d;
attempts--;
if (attempts == 0) {
exit(1);
}
}
while (abs(b - a) >= eps) {
x1 = (a + b) / 2;
if (f(x1) == 0.0) break;
else if (f(x1) * f(a) <= 0) b = x1;
else a = x1;
}
cout << "x1: " << x1 << endl;
/*cout << "x2: " << x2 << endl; - how to get this number?*/
return 0;
}
For the full equation c * (x * x) + 2 * x - d = 0, solve for x (using a
solver with explanations):
Possible xs:
(-1. + std::sqrt(c * d + 1.)) / c and -((std::sqrt(c * d + 1.) + 1.) / c)

Why this code is not working for sum of even integers?

I am trying to find the sum of even integers, but it is not working. It only works for sum of integers after removing the if condition.
main()
{
int number, sum, upperlimit;
number=1;
sum=0;
std :: cout << "Please enter the number of digits:";
std :: cin >> upperlimit;
while(number<=upperlimit)
{
if(number%2==0)
{
sum = sum + number;
number = number + 1;
}
}
std :: cout << "sum of 1st " << upperlimit <<" Even digits is:" << sum;
}
When I am entering the digits not going any where.
if(number%2==0)
{
sum = sum + number;
number = number + 1;
}
Having the number+=1condition inside that ifmeans that it will only move on if the number is even. So once you reach 1, for example, it will stay as a 1 for ever.
main()
{
int number, sum, upperlimit;
number=1;
sum=0;
std :: cout << "Please enter the number of digits:";
std :: cin >> upperlimit;
while(number<=upperlimit)
{
if(number%2==0)
{
sum = sum + number;
}
number = number + 1;
}
std :: cout << "sum of 1st " << upperlimit <<" Even digits is:" << sum;
}

Minimum number of operations to get from source to target.

I came across this question during an interview -
Convert a number source to target in the minimum number of operations.
Allowed Operations
Multiplied by 2.
Addition by 1.
subtraction by 1.
0 < source, target <= 1000.
I tried going the naive recursive route(O(3^n)) ie. subtract 1, add 1 and multiply by 2 at each level to try and find a solution that I could extend to Dynamic Programming but couldnt because of an infinite loop.
//Naive approach Via Recursion
int minMoves(int source, int target){
if(source <1 || source > target){
return -1;
}
int moves =0;
// Potential infinite loop - consider 3,6-> 2,6- >1,6->(0,6)x (2,6)->1,6->(0,6)x (1,6)->(0,6)x (2,6)->1,6..
int movesLeft = minMoves(source -1, target) ==-1? Integer.MAX_VALUE:minMoves(source -1, target);
int movesRight = minMoves(source +1, target) ==-1? Integer.MAX_VALUE:minMoves(source +1, target);
int moves2X = minMoves(2*source, target) ==-1? Integer.MAX_VALUE:minMoves(2*source, target);
moves = 1+ Math.min(Math.min(movesRight,movesLeft), moves2X);
return moves;
}
Any ideas on how I can tweak my solution? Or possibly a better way to solve it?
If you think about your solution like a graph traversal, where each node is an intermediate value you can produce, your recursive solution is like a depth first search (DFS). You'll have to fully expand until you've tried all solutions from that "branch" of the search space before you can proceed anywhere else. If you have an infinite loop, this means it will never terminate even if a shorter path exists, and even if you don't have an infinite loop, you still have to search the rest of the solution space to make sure its optimal.
Instead, consider an approach similar to breadth first search (BFS). You expand outward uniformly, and will never search a path longer than the optimal solution. Just use FIFO queue to schedule which node to access next. This is the approach I've taken with my solver.
from queue import Queue
def solve(source, target):
queue = Queue()
path = [source]
queue.put(path)
while source != target:
queue.put(path + [source * 2])
queue.put(path + [source + 1])
queue.put(path + [source - 1])
path = queue.get()
source = path[-1]
return path
if __name__ == "__main__":
print(solve(4,79))
One way in which you can speed up(and possibly fix) this code, while maintaining the recursive implementation, is to use memoization.
The issue here is that you are recalculating the same value many times. Instead you can use a map to store the results that you already calculated, and reuse them when you need it again.
This problem can be solved constructively. First, the easy cases. If s=t, the answer is 0. If s > t, the answer is s-t because subtraction by 1 is the only operation that lowers s, and the other two can only increase the number of subtractions required.
Now let's assume s < t. Since s>0 is given, doubling will always be the fastest way to increase (if s is 1, it's tied with incrementing). So if the challenge was to make s >= t, the answer would always be the number of doublings required to do that. This procedure may overshoot t, but the first doubling greater than t and the last doubling not greater than t must be within a factor of 2 of t.
Let's look at the effect of when we do an addition or subtraction. First, look only at addition:
(((s*2) * 2) * 2) + 1 = 8s + 1
vs:
((((s+1)*2) * 2) * 2) = 8s + 8
Putting an addition before n doublings makes the final result 2^n bigger. So consider if s is 3 and t is 8. The last double not bigger than 8 is 6. This is 2 off, so if we put an addition 1 double before the last double, we get what we want: (3+1) * 2. Alternatively we could try overshooting to the first double greater than 8, which is 12. This is 4 off, so we need to put a subtraction two doublings before the last : (3-1)*2*2 = 8
In general if we are x below the target, we need to put a +1 at n doublings before the last if the binary representation of x has a 1 at the nth place.
Similarly, if we are x above the target, we do likewise with -1's.
This procedure won't help for the 1's in x's binary representation that are at a position more than the number of doublings there are. For example, if s = 100, t=207, there is only 1 doubling to do, but x is 7, which is 111. We can knock out the middle one by doing an addition first, the rest we have to do one by one (s+1)*2 + 1 + 1 + 1 + 1 + 1.
Here is an implementation that has a debug flag that also outputs the list of operations when the flag is defined. The run time is O(log(t)):
#include <iostream>
#include <string>
#include <sstream>
#define DEBUG_INFO
int MinMoves(int s, int t)
{
int ans = 0;
if (t <= s)
{
return s - t; //Only subtraction will help
}
int firstDoubleGreater = s;
int lastDoubleNotGreater = s;
int nDouble = 0;
while(firstDoubleGreater <= t)
{
nDouble++;
lastDoubleNotGreater = firstDoubleGreater;
firstDoubleGreater *= 2;
}
int d1 = t - lastDoubleNotGreater;
int d2 = firstDoubleGreater - t;
if (d1 == 0)
return nDouble -1;
int strat1 = nDouble -1; //Double and increment
int strat2 = nDouble; //Double and decrement
#ifdef DEBUG_INFO
std::cout << "nDouble: " << nDouble << "\n";
std::stringstream s1Ops;
std::stringstream s2Ops;
int s1Tmp = s;
int s2Tmp = s;
#endif
int mask = 1<<strat1;
for(int pos = 0; pos < nDouble-1; pos++)
{
#ifdef DEBUG_INFO
if (d1 & mask)
{
s1Ops << s1Tmp << "+1=" << s1Tmp+1 << "\n" << s1Tmp+1 << "*2= " << (s1Tmp+1)*2 << "\n";
s1Tmp = (s1Tmp + 1) * 2;
}
else
{
s1Ops << s1Tmp << "*2= " << s1Tmp*2 << "\n";
s1Tmp = s1Tmp*2;
}
#endif
if(d1 & mask)
strat1++;
d1 = d1 & ~mask;
mask = mask >> 1;
}
strat1 += d1;
#ifdef DEBUG_INFO
if (d1 != 0)
s1Ops << s1Tmp << " +1 " << d1 << " times = " << s1Tmp + d1 << "\n";
#endif
mask = 1<<strat2;
for(int pos = 0; pos < nDouble; pos++)
{
#ifdef DEBUG_INFO
if (d2 & mask)
{
s2Ops << s2Tmp << "-1=" << s2Tmp-1 << "\n" << s2Tmp-1 << "*2= " << (s2Tmp-1)*2 << "\n";
s2Tmp = (s2Tmp-1)*2;
}
else
{
s2Ops << s2Tmp << "*2= " << s2Tmp*2 << "\n";
s2Tmp = s2Tmp*2;
}
#endif
if(d2 & mask)
strat2++;
d2 = d2 & ~mask;
mask = mask >> 1;
}
strat2 += d2;
#ifdef DEBUG_INFO
if (d2 != 0)
s2Ops << s2Tmp << " -1 " << d2 << " times = " << s2Tmp - d2 << "\n";
std::cout << "Strat1: " << strat1 << "\n";
std::cout << s1Ops.str() << "\n";
std::cout << "\n\nStrat2: " << strat2 << "\n";
std::cout << s2Ops.str() << "\n";
#endif
if (strat1 < strat2)
{
return strat1;
}
else
{
std::cout << "Strat2\n";
return strat2;
}
}
int main()
{
int s = 25;
int t = 193;
std::cout << "s = " << s << " t = " << t << "\n";
std::cout << MinMoves(s, t) << std::endl;
}
Short BFS algorithm. It finds the shortest path in graph where every vertex x is connected to x + 1, x - 1 and x * 2; O(n)
#include <bits/stdc++.h>
using namespace std;
const int _MAX_DIS = 2020;
const int _MIN_DIS = 0;
int minMoves(int begin, int end){
queue<int> Q;
int dis[_MAX_DIS];
fill(dis, dis + _MAX_DIS, -1);
dis[begin] = 0;
Q.push(begin);
while(!Q.empty()){
int v = Q.front(); Q.pop();
int tab[] = {v + 1, v - 1, v * 2};
for(int i = 0; i < 3; i++){
int w = tab[i];
if(_MIN_DIS <= w && w <= _MAX_DIS && dis[w] == -1){
Q.push(w);
dis[w] = dis[v] + 1;
}
}
}
return dis[end];
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout << minMoves(1, 1000);
return 0;
}

Bigger value without greater than or less than operators

this is just for fun.
Can you give me a way to determine the greater value of two numbers? You can use other operators except greater than or less than operator.
Use any tool you like (programming language, pencil paper, etc.).
And another thing you cannot do the following:
int main()
{
int num1, num2;
cin >> num1 >> num2;
if( (num1-num2) + abs(num1-num2) )
cout << num1 << " is bigger" << endl;
else
cout << num2 << " is bigger" << endl;
return 0;
}
Well, if you assume two's complement arithmetic:
int highBit = ~INT_MAX;
int rslt = num1 - num2;
if (rslt & highBit)
// num2 > num1
else if (rslt)
// num1 > num2
else
// num1 == num2
This will only work when both numbers are positive. For example, if num1 is positive and num2 is negative, then num1-num2 could overflow, causing the result to be negative, which would erroneously report that num2 > num1. If both numbers are negative, this will report the opposite (i.e. -12 will be reported greater than -1).
Ok, I would transform them to binary code and walk from the left byte-by-byte. Whichever first has 1 when the other one has not (means: has 0), this is greater.
So if you go from left to right and:
both have 0 at the current position: go to the next position,
both have 1 at the current position: go to the next position,
first one has 1 at the current position and the second one has 0: the first one is bigger,
second one has 1 at the current position and the first one has 0: the second one is bigger,
If you determine that 3. or 4. is matched, you have the result. If 1. or 2. is matched, repeat the same for the next position. If you have walked through all the bytes and did not determine that one of them is bigger, then both are equal.
#include <algorithm>
cout << std::max(num1, num2) << " is bigger" << endl;
Logarithm of a negative value is undefined; different languages/frameworks process it differently. This solution is for C#:
using System;
public class Test
{
public static bool gt( double a, double b ) {
return Double.IsNaN(Math.Log(b - a));
}
public static void report_gt( double a, double b) {
if( gt(a,b) )
Console.WriteLine("{0} is greater than {1}", a, b);
else
Console.WriteLine("{0} is less than or equal to {1}", a, b);
}
public static void Main()
{
Test.report_gt(-1, 0);
Test.report_gt(1, 0);
Test.report_gt(1, 2);
Test.report_gt(-1, -2);
}
}
Output:
-1 is less than or equal to 0
1 is greater than 0
1 is less than or equal to 2
-1 is greater than -2
A similar solution for C could use floating point exceptions. Unfortunately, C++ does not throw an exception for a negative argument to log2() for a nice try-catch solution in this fun contest :).
This works for positive integers:
#! /usr/bin/python
import math
def bigger (a, b):
length = int (math.log (a, 2) + math.log (b, 2) + 1)
a = toFixedBitString (a, length)
b = toFixedBitString (b, length)
print a, b
while a:
if a [0] == '1' and b [0] == '0':
print "First number is bigger."
return
if a [0] == '0' and b [0] == '1':
print "Second number is bigger."
return
a = a [1:]
b = b [1:]
print "Numbes are equal."
def toFixedBitString (a, length):
retVal = ''
for x in range (length): retVal = ['0', '1'] [a >> x & 1] + retVal
return retVal
Warning: Untested code.
num2 ~= num2;
++num2;
num1 += num2;
rotate_left(num1, 1);
if (num1 == 0)
std::cout << "num1 == num2";
else if (num1 & 1)
std::cout << "num1 < num2";
else
std::cout << "num1 > num2";
I haven't thought about it a lot, but there are probably combinations that will fail due to overflow.
#include <limits>
int a = -1;
int b = -11;
// true for positive difference
bool dpos = ~(unsigned int)(a - b) >> numeric_limits<int>::digits; // 31 for 32-bit
cout << "Problem : a = " << a << " and b = " << b << endl;
if (a == b)
cout << " a == b " << endl;
else if (dpos)
cout << " a > b " << endl;
else
cout << " a < b " << endl;
SSE 4.1 solution
#include <smmintrin.h>
#include <iostream>
using namespace std;
int main()
{
int num1, num2, test;
__m128i v1, v2, vcmp;
cin >> num1 >> num2;
v1 = _mm_set1_epi32(num1);
v2 = _mm_set1_epi32(num2);
vcmp = _mm_cmpgt_epi32(v1, v2);
test = _mm_testz_si128(vcmp, vcmp);
if (test == 0)
{
cout << "num1 is bigger" << endl;
}
else
{
cout << "num2 is bigger" << endl;
}
return 0;
}
$ g++ -Wall -msse4.1 cmpgt.cpp -o cmpgt
$ ./cmpgt
-10 10
num2 is bigger
$ ./cmpgt
10 -10
num1 is bigger

Resources