Unclear Output From Function of Two Variables in C++ - function

I have written a function which takes two doubles and outputs some polynomial expression. This is a prototype for something (much) more complicated that I need to do later on. The code should be fairly straightforward, but I must be doing something wrong, because the output makes no sense. The function returns 0 or -0, no matter what values I pass to the arguments.
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
double funcTwoVars(double x, double y){
double result = (1/6)*(1/x - 3*x/4)*y;
return result;
}
int main(){
double fxy = funcTwoVars(10,10);
cout << fixed << setprecision(6) << fxy << endl;
return 0;
}
When I run it, the output is the following:
christian#christian-HP-Pavilion-x360-Convertible:~/code/HYBRIDS$ g++ functionOfTwoVars.cpp -o functionOfTwoVars
christian#christian-HP-Pavilion-x360-Convertible:~/code/HYBRIDS$ ./functionOfTwoVars
-0.000000
I have no idea why it does not output the correct value. Any suggestions?
Thanks.

I actually was able to find the answer. The problem was that I was using int values in a function that asks for doubles. That was a stupid mistake on my part.

Related

difference between "rint" and "nearbyint"?

what is the difference between rint and nearbyint?
Will they give some different output in some cases?
If not, is there a difference in the concept of calculations?
Since these are both C functions, we can check the man page for both of these. An excerpt:
The nearbyint() functions round their argument to an integer value in floating-point format, using the current rounding direction (see fesetround(3)) and without raising the inexact exception.
The rint() functions do the same, but will raise the inexact exception (FE_INEXACT, checkable via fetestexcept(3)) when the result differs in value from the argument.
In other words, rint allows you to do error checking while nearbyint does not. An example of error-checking:
#include <iostream>
#include <cmath>
#include <cfenv>
int main()
{
std::feclearexcept(FE_INEXACT);
double a = std::rint(93819.249);
if (!std::fetestexcept(FE_INEXACT))
std::cerr << "Bad rounding\n";
else
std::cout << a << '\n';
}

thrust reduction result on device memory

Is it possible to leave the return value of a thrust::reduce operation in device-allocated memory? In case it is, is it just as easy as assigning the value to a cudaMalloc'ed area, or should I use a thrust::device_ptr?
Is it possible to leave the return value of a thrust::reduce operation in device-allocated memory?
The short answer is no.
thrust reduce returns a quantity, the result of the reduction. This quantity must be deposited in a host resident variable:
Take for example reduce, which is synchronous and
always returns its result to the CPU:
template<typename Iterator, typename T>
T reduce(Iterator first, Iterator last, T init);
Once the result of the operation has been returned to the CPU, you can copy it to the GPU if you like:
#include <iostream>
#include <thrust/device_vector.h>
#include <thrust/reduce.h>
int main(){
thrust::device_vector<int> data(256, 1);
thrust::device_vector<int> result(1);
result[0] = thrust::reduce(data.begin(), data.end());
std::cout << "result = " << result[0] << std::endl;
return 0;
}
Another possible alternative is to use thrust::reduce_by_key which will return the reduction result to device memory, rather than copy to host memory. If you use a single key for your entire array, the net result will be a single output, similar to thrust::reduce
Yes, it should be possible by using thrust::reduce_by_key instead with a thrust::constant_iterator supplied for the keys.

Function return not returning a value to main

So, this is my first assignment fiddling with functions in C++ - which I thought I understood being as they're rather similar to methods with C#. But, although my main calls my function fine, and the function runs and returns to the main - It doesn't send back the variable information that it called when it returns. I'm not exactly sure what I've done wrong - it appears to be set up appropriately (IE - Like the sample code in my book for a return)
Here's the main code...
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main()
{
double retrieveSales=0, sales1=0, sales2=0, sales3=0, sales4=0;
string division, division2, division3, division4;
double getSales(string);
cout<<"Enter division.\n";
cin>>division;
getSales(division);
retrieveSales=sales1;
cout<<"Enter second division.\n";
cin>>division2;
getSales(division2);
retrieveSales=sales2;
cout<<"Print Sales"<<sales2;
cout<<"Enter third division.\n";
cin>>division3;
getSales(division3);
retrieveSales=sales3;
cout<<"Print Sales"<<sales3;
cout<<"Enter fourth division.\n";
cin>>division4;
getSales(division4);
retrieveSales=sales4;
cout<<"Print Sales"<<sales4;
system("pause");
return 0;
}
And here's the code for the function that it calls
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
double getSales(string division)
{
double retrieveSales;
cout<<"What are the sales for "<<division<<endl;
cin>>retrieveSales;
while(retrieveSales<0.0)
{
cout<<"Please enter a valid sales amount no less than $0.00.\n";
cin>>retrieveSales;
}
system("pause");
return retrieveSales;
}
How do I get my function to return the value of retrieveSales to retrieveSales in the main?
In this line:
getSales(division);
You are discarding the return value, you need to assign it to a variable:
sales1 = getSales(division);

Parsing HTML form data

When I simply print the query I get:
one=1&two=2&three=3&four=3&five=3&six=3
Still not working!!!! I am about to go nuts.
#include <stdio.h>
#include <stdlib.h>
int main(void){
char *data;
float prices[] = {1, 2, 3, 4, 5, 6};
int a, b, c, d, e, f;
printf("%s%c%c\n",
"Content-Type:text/html;charset=iso-8859-1",13,10);
printf("<title>Bill</title>\n");
printf("<h3 align=center >Bill</h3>\n");
data = getenv("QUERY_STRING");
if(data == NULL){
printf("<p>Error!</p>");
} else {
printf("%s", data);
sscanf(data, "one=%d&two=%d&three=%d&four=%d&five=%d&six=%d", &a, &b, &c, &d, &e, &f);
}
return 0;
}
Is there an easy and not error prone way to deal with this issue?
Yes; there exist various C libraries, such as cgic and C CGI, that handle common CGI tasks like this one. (If neither of those is to your taste, try Googling "C CGI library" for other options.)
An easy way to parse this string is using sscanf. For this example:
one=2&two=1
you could use:
int one, two;
sscanf(data, "one=%d&two=%d", &one, &two);
More information about sscanf here: http://www.cplusplus.com/reference/clibrary/cstdio/sscanf/
you don't parse anything, so how do you expect to get the values? (Like others said, the most simple way is to use sscanf. note that it will work only if you know the names and order of the arguments, and that you should unescape characters handly after that.)
Your code have some bad things, like if (data==NULL) ... printf("%s",data) (if data may be NULL, why do you print it), and using quantities without allocating space to it.

fast CUDA thrust custom comparison operator

I'm evaluating CUDA and currently using Thrust library to sort numbers.
I'd like to create my own comparer for thrust::sort, but it slows down drammatically!
I created my own less implemetation by just copying code from functional.h.
However it seems to be compiled in some other way and works very slowly.
default comparer: thrust::less() - 94ms
my own comparer: less() - 906ms
I'm using Visual Studio 2010. What should I do to get the same performance as at option 1?
Complete code:
#include <stdio.h>
#include <cuda.h>
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/generate.h>
#include <thrust/sort.h>
int myRand()
{
static int counter = 0;
if ( counter++ % 10000 == 0 )
srand(time(NULL)+counter);
return (rand()<<16) | rand();
}
template<typename T>
struct less : public thrust::binary_function<T,T,bool>
{
__host__ __device__ bool operator()(const T &lhs, const T &rhs) const {
return lhs < rhs;
}
};
int main()
{
thrust::host_vector<int> h_vec(10 * 1000 * 1000);
thrust::generate(h_vec.begin(), h_vec.end(), myRand);
thrust::device_vector<int> d_vec = h_vec;
int clc = clock();
thrust::sort(d_vec.begin(), d_vec.end(), less<int>());
printf("%dms\n", (clock()-clc) * 1000 / CLOCKS_PER_SEC);
return 0;
}
The reason you are observing a difference in performance is because Thrust is implementing the sort with different algorithms depending on the arguments provided to thrust::sort.
In case 1., Thrust can prove that the sort can be implemented in linear time with a radix sort. This is because the type of the data to sort is a built-in numeric type (int), and the comparison function is the built-in less than operation -- Thrust recognizes that thrust::less<int> will produce the equivalent result as x < y.
In case 2., Thrust knows nothing about your user-provided less<int>, and has to use a more conservative algorithm based on a comparison sort which has different asymptotic complexity, even though in truth your less<int> is equivalent to thrust::less<int>.
In general, user-defined comparison operators can't be used with more restrictive, faster sorts which manipulate the binary representation of data such as radix sort. In these cases, Thrust falls back on a more general, but slower sort.