-8

I am having this issue in C++ with numbers:

Given two numbers:

numb1 = 0.000171438
numb2 = 11666;

If I do

fillweight= float(numb1 * numb2)

I get as an answer "1", while if I do it like

fillweight = 0.000171438 * 11666

I get the "1.9999" answer correctly on screen - What is the problem with passing in floats? I ve tried also with something like

fillweight = float(float(numb1) * float(numb2))

But they're always the same answer.

David G
  • 94,763
  • 41
  • 167
  • 253
Alex
  • 191
  • 1
  • 9
  • 7
    You didn't post the type of the numbers... – Pubby Jan 21 '13 at 21:23
  • 1
    At a guess I'd say either `fillweight` `numb1` or `numb2` are defined as type `int` or `long`. – jstine Jan 21 '13 at 21:24
  • 6
    Please post a complete, short program that demonstrates your problem. See http://sscce.org for more info. When *I* create a program using your comments as a guide, I get `2` for each of the expressions you list. See [here](http://ideone.com/7CIM70) for my short example program. – Robᵩ Jan 21 '13 at 21:24
  • 1
    I certainly get 1.99996 for the calculation above when using float. – Mats Petersson Jan 21 '13 at 21:28
  • 1
    I also verified that every type-correct combination of either float or double with those input numbers produces the correct results on gcc and visual studio compilers. Your problem is elsewhere in your code. – jstine Jan 21 '13 at 21:33

3 Answers3

2

In

fillweight = 0.000171438 * 11666

The first number is a double constant and the multiplication is done using double-precision arithmetic (11666 will be converted to double) This will happen likely in compile time.

fillweight = 0.000171438f * 11666f

Will be the same as

fillweight = float(float(numb1) * float(numb2))

if numb1 and numb2 are floats.

Although this does not solve your problem. But without a minimal working example there is nothing more to say than to watch out for your types.

Csq
  • 5,775
  • 6
  • 26
  • 39
  • 4
    But the number the OP's showing aren't extreme enough for using floats to be the problem. – Winston Ewert Jan 21 '13 at 21:26
  • This is not a floats vs doubles problem. This is a problem of the OP's result being truncated to an integer somewhere along the way. – jstine Jan 21 '13 at 21:28
  • Well, that is true. Really need the types of the OP's variables to tell more. – Csq Jan 21 '13 at 21:29
  • I dont get it...why it returns the correct answer for values smaller than 10000 in the second term ? – Alex Jan 21 '13 at 21:29
0

Floats have significantly less precision available than doubles - in the one that "works" the literals are interpreted as doubles.

A lot of info on that can be found here:

float vs. double precision

In light checking up on what a float can hold - chances are this difference isn't what's causing your problem. I'd bet the issue is that the variable you are trying to hold the result in is perhaps an int or some other inappropriate type for the multiplication being performed.

Community
  • 1
  • 1
PinkElephantsOnParade
  • 6,452
  • 12
  • 53
  • 91
0

You are possibly seeing a rounding in your output method. For example, take the following code:

#include "stdafx.h"
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
    std::cout.precision(5);
    std::cout << "Precision(5)" << std::endl;
    {
    float numb1 = 0.000171438;
    float numb2 = 11666;
    float fillweight = float(numb1 * numb2);
    std::cout << "Test #1: fillweight = " << fillweight << " numb1 = " << numb1 << " numb2 = " << numb2 << std::endl;
    }
    {
    float numb1 = 0.000171438;
    float numb2 = 9999;
    float fillweight = float(numb1 * numb2);
    std::cout << "Test #2: fillweight = " << fillweight << " numb1 = " << numb1 << " numb2 = " << numb2 << std::endl;
    }
    std::cout.precision(10);
    std::cout << "Precision(10)" << std::endl;
    {
    float numb1 = 0.000171438;
    float numb2 = 11666;
    float fillweight = float(numb1 * numb2);
    std::cout << "Test #3: fillweight = " << fillweight << " numb1 = " << numb1 << " numb2 = " << numb2 << std::endl;
    }
    {
    float numb1 = 0.000171438;
    float numb2 = 9999;
    float fillweight = float(numb1 * numb2);
    std::cout << "Test #4: fillweight = " << fillweight << " numb1 = " << numb1 << " numb2 = " << numb2 << std::endl;
    }

    int i;
    std::cin >> i;
    return 0;
}

And you will get this output:

Precision(5)
Test #1: fillweight = 2 numb1 = 0.00017144 numb2 = 11666
Test #2: fillweight = 1.7142 numb1 = 0.00017144 numb2 = 9999
Precision(10)
Test #3: fillweight = 1.999995708 numb1 = 0.0001714380051 numb2 = 11666
Test #4: fillweight = 1.714208603 numb1 = 0.0001714380051 numb2 = 9999
DanS
  • 1,221
  • 1
  • 9
  • 4