Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8194401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:48:41+00:00 2026-06-07T04:48:41+00:00

I am doing the following on the GPU float decPart = valAtIndex – (int)valAtIndex;

  • 0

I am doing the following on the GPU

float decPart = valAtIndex - (int)valAtIndex;
int docID = decPart * numDocs;

where valAtIdex if of type float and numDocs also of type float. For my case, decPart was 0.2 and numDocs was 10. However, when I print docID, it is printed as 1 (it should be 2). Can somebody please tell me where am I making a mistake?

Below is the full method if it helps

__global__ 
void finalNc(float* scSortedCounts, int* pos, int* maxCountEx, float numDocs, 
             int lengthStreamCompacted, int* finalNc, int actualLengthPos, 
             float* val, int* docIndex, int* acV ,int* ptwrite,int* diff,
             int* posIndex)
{ 
    int index = blockDim.x * blockIdx.x + threadIdx.x; 
    if(index < lengthStreamCompacted){ 
        float valAtIndex = scSortedCounts[index]; 
        float decPart = valAtIndex - (int)valAtIndex; 
        int docID = decPart * numDocs; 
        int actualCount = (int)valAtIndex; 
        int placeToWrite = maxCountEx[docID] + actualCount; 
        if( index == (lengthStreamCompacted -1 )){ 
            finalNc[placeToWrite] = actualLengthPos - pos[index]; 
        }else{ 
            finalNc[placeToWrite] = pos[index + 1] + pos[index]; 
        } 
    } 
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T04:48:43+00:00Added an answer on June 7, 2026 at 4:48 am

    For the case you have mentioned, it appears that the result you are seeing is correct, and the source of your confusion is down to IEEE single precision representation of the intermediate results in your calculations (and perhaps rounding in printing or displaying of those intermediate results).

    For the example you provide, a value of 0.2f is not exactly representable as a binary32 value. The two possible values are either

    3E4CCCCC (1.99999988079071044921875E-1) 
    

    or

    3E4CCCCD (2.0000000298023223876953125E-1)
    

    If the first value was the actual value of docID, then the intermediate calculation you mention should produce 1 (which is what you observed). If it was second value, the result would be 2. This is absolutely expected behaviour.

    To illustrate the effect of IEEE rounding modes and help put your mind at ease that there is no error here, have a look at the following example code, which performs the calculation you are asking about with one of three possible float to integer conversions – plain truncation, IEEE 754 round towards minus infinity, and IEEE 754 round towards plus infinity. I have templated the kernel and run it for each case on a set of random float values between 1 and 10. You can compile and run it for yourself and verify that the plain truncation (as in your code) really does work as intended, as well as the behaviour of the IEEE compliant “round up” and “round down” conversions on the output.

    #include <thrust/host_vector.h>
    #include <thrust/device_ptr.h>
    #include <thrust/device_malloc.h>
    #include <thrust/generate.h>
    #include <cstdlib>
    #include <cstdio>
    
    template<int version>
    __global__
    void kernel(const float *inputs, int *outputs, int numDocs, int N)
    {
        int index = blockDim.x * blockIdx.x + threadIdx.x; 
    
        if(index < N){ 
            float valAtIndex = inputs[index];
            int intPart;
            switch(version) {
                case 2:
                    intPart = __float2int_ru(valAtIndex);
                    break;
                case 1:
                    intPart = __float2int_rd(valAtIndex);
                    break;
                case 0:
                default:
                    intPart = int(valAtIndex);
                    break;
            }
            float decPart = valAtIndex - intPart;
            int docID = decPart * numDocs;
            outputs[index] = docID;
        }
    }
    
    inline float frand(){
        return 1.0f + 9.0f * ((float)rand()/(float)RAND_MAX);
    }
    
    int main(void)
    {
        const size_t N = 100;
        const int numdocs = 10;
    
        srand(time(NULL));
    
        thrust::host_vector<float> values(N);
        thrust::host_vector<int> outputs(3*N);
        std::generate(values.begin(), values.end(), frand);
    
        thrust::device_ptr<float> in = thrust::device_malloc<float>(N);
        float * _in = thrust::raw_pointer_cast(in);
        thrust::copy(values.begin(), values.end(), in);
    
        thrust::device_ptr<int> out = thrust::device_malloc<int>(3*N);
        int * _out = thrust::raw_pointer_cast(out);
    
        kernel<0><<<1,128>>>(_in, _out, numdocs, N);
        kernel<1><<<1,128>>>(_in, _out+N, numdocs, N);
        kernel<2><<<1,128>>>(_in, _out+(2*N), numdocs, N);
    
        thrust::copy(out, out+3*N, outputs.begin());
    
        for(int i=0; i<N; i++)
            printf("%.10f %d %d %d\n", 
                    values[i], outputs[i], outputs[N+i], outputs[2*N+i]);
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing the following: <script type=text/javascript> $(function(){ $(#ipad).submit(function() { $.post(ipadcheck.php, $(#ipad).serialize(), function(data) {
I am doing the following: $(#fb_friends).append(<div style=\width:150px;float:left;font-size:11px;color:White;\>); $(#fb_friends).append(<input type=\checkbox\ name=\friends\ value=\1244524\ />); $(#fb_friends).append(<img src=\http://graph.facebook.com/
I am doing the following: __shared__ int exForBlockLessThanP = totalElementLessThanPivotEntireBlock[blockIdx.x]; where totalElementLessThanPivotEntireBlock is an
I my index lets say I have field field named full_name. I'm doing following
While doing the following simple example, I found the following difficulties As the title
After doing the following test: for( i = 0; i < 3000000; i++ )
I'm currently doing the following to compensate for boolean's not mapping well to radio
I need help doing the following: a preprocessor macro label(x) shall output #x, e.g.,
I'm currently doing the following as part of my iPhone application NSArray *paths =
I find myself doing the following a lot, and i don't know if there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.