Double Value Returns 0

Double value returns 0

That's because 1 and 3 are treated as integers when you don't specify otherwise, so 1/3 evaluates to the integer 0 which is then cast to the double 0. To fix it, try (1.0/3), or maybe 1D/3 to explicitly state that you're dealing with double values.

C - double result returning the value of 0

Because it's performing integer devision since a and b are ints, try this

double result = (float) a / (float) b;

Division in double variable returning always zero

The result of 80/100 (both integers) is always 0.

Change it to 80.0/100.0

Operation with double value returning 0. Can't understand why

It's probably a mistake using %1f for getting the floats since you'll only get one digit. You probably want to use l (ell) rather than 1 (wun).

In addition, a function like h_mean should do what it says, which is work out the harmonic mean. It should not be getting user input, especially since the variables are passed to it from main. You would be better off starting with something like (with comments explaining why):

#include <stdio.h>

// I'm a fan of DRY, so I tend to put definintions first. That
// way, I don't have to maintain definition *and* declaration.

double h_mean(double i, double j) {
// I like explicit doubles, like 1.0. I also like spaces :-)

double inv_num = (1.0 / i + 1.0 / j) / 2.0;
double inv_result = 1.0 / inv_num;
return inv_result;
}

int main(void) {
double i, j;
// Main should be getting input, leaving h_mean to do one thing,
// and one thing well.
// Also use lf (ell) rather than 1f (wun), and check that scanf
// worked okay.
// And I like data entry on the prompt line.

printf("Enter 2 numbers: ");
if (scanf("%lf %lf", &i, &j) != 2) {
puts("Invalid input");
return 1;
}
double hMean = h_mean(i, j);

// No need for lf in printf, that's only needed for scanf.

printf("%f\n", hMean);

return 0;
}

With that code, I get (with a sample run):

Enter 2 numbers: 3.14159265359 2.71828182846
2.914647

Double returns 0

Your problem is at

log10(1 + val / 700)

That's using an integer division instead of a real division. Try with

log10(1 + val / 700.0)

Why is my double or int value is always 0 after division?

Your current calculation only involves integers and so will be affected by integer division (which truncates the result to the nearest integer value).

(currPixel / modifier) * newValue
| |
---------------integer division e.g. 10/3 = 3, not 3.333

The result is then cast to double, but the accuracy is lost before this point.

Consider the following:

#include <iostream>
using namespace std;

int main() {
int val1 = 10;
int val2 = 7;
int val3 = 9;

double outval1 = (val1 / val2) * val3;
double outval2 = ((double)val1 / val2) * val3;
cout << "without cast: " << outval1 << "\nwith cast: "<< outval2 << std::endl;

return 0;
}

The output of this is:

without cast: 9
with cast: 12.8571

See it here

Note that the cast has to be applied in the right place:

(double)(val1 / val2) * val3 == 9.0      //casts result of (val1/val2) after integer division
(val1 / val2) * (double)val3 == 9.0 //promotes result of (val1/val2) after integer division
((double)val1 / val2) * val3 == 12.8571 //promotes val2 before division
(val1 / (double)val2) * val3 == 12.8571 //promotes val1 before division

Due to promotion of the other operands, if in doubt you can just cast everything and the resulting code will be the same:

((double)val1 / (double)val2) * (double)val3 == 12.8571  

It is a little more verbose though.

Why does 1 / 2 == 0 using double?

It's because of the data type.

When you do 1/2 that is integer division because two operands are integers, hence it resolves to zero (0.5 rounded down to zero).

If you convert any one of them to double, you'll get a double result.

double d = 1d/2;

or

double d = 1/2.0;

Why casting big double value in sbyte returns 0 in C#?

According to the C# language specification,

For a conversion from float or double to an integral type, the processing depends on the overflow checking context in which the conversion takes place:

Without using the checked or unchecked operators, by default the overflow checking context is unchecked, so we look at:

In an unchecked context, the conversion always succeeds, and proceeds as follows.

  • If the value of the operand is NaN or infinite, the result of the conversion is an unspecified value of the destination type.

  • Otherwise, the source operand is rounded towards zero to the nearest integral value. If this integral value is within the range of the destination type then this value is the result of the conversion.

  • Otherwise, the result of the conversion is an unspecified value of the destination type.

Here, the values are neither NaN nor infinite. When rounded towards zero, they are not in the valid range of sbyte, which is -128 to 127, therefore the last bullet point applies, which means that the result of such a cast is unspecified.

In other words, the result of this cast depends on which compiler you are using. Different compilers could do different things, and they will still be called C# compilers. It is likely that whatever compiler that you are using just thought it'd be a better idea to return 0 for the conversion when the value to convert is very far away the lower/upper bound.



Related Topics



Leave a reply



Submit