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

  • Home
  • SEARCH
  • 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 3350324
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:46:23+00:00 2026-05-18T01:46:23+00:00

I have a program to calculate a weighted product (I adapted from another program

  • 0

I have a program to calculate a weighted product (I adapted from another program I found). But I can’t figure out how to initialize the total_data part of the product_state struct to one so that I don’t always end up with a product of zero. Right now it’s keeping the default zero value so that all of the values I pass in from SQLite get zeroed out.

I took some C++ in college, but never deep into structs, and coming back to this is outside my wheelhouse. Thanks!

/* product.c */

#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1;

#include <stdlib.h>

typedef struct product_state_s {
   double   total_data;  /* sum of (data * weight) values */
   double   total_wt;    /* sum of weight values */
} product_state;

static void product_step( sqlite3_context *ctx, int num_values, sqlite3_value **values )
{
    double         row_wt = 1.0;
    int            type;
    product_state   *st = (product_state*)sqlite3_aggregate_context( ctx,
                                               sizeof( product_state ) );
    if ( st == NULL ) {
        sqlite3_result_error_nomem( ctx );
        return;
    }

    /* Extract weight, if we have a weight and it looks like a number */
    if ( num_values == 2 ) {
        type = sqlite3_value_numeric_type( values[1] );
        if ( ( type == SQLITE_FLOAT )||( type == SQLITE_INTEGER ) ) {
            row_wt = sqlite3_value_double( values[1] );
        }
    }

    /* Extract data, if we were given something that looks like a number. */
    type = sqlite3_value_numeric_type( values[0] );
    if ( ( type == SQLITE_FLOAT )||( type == SQLITE_INTEGER ) ) {
        st->total_data *= row_wt * sqlite3_value_double( values[0] );
        st->total_wt   += row_wt;
    }
}

static void product_final( sqlite3_context *ctx )
{
    double         result = 0.0;
    product_state   *st = (product_state*)sqlite3_aggregate_context( ctx,
                                               sizeof( product_state ) );
    if ( st == NULL ) {
        sqlite3_result_error_nomem( ctx );
        return;
    }

    if ( st->total_wt != 0.0 ) {
        result = st->total_data / st->total_wt;
    }
    sqlite3_result_double( ctx, result );
}

int product_init( sqlite3 *db, char **error, const sqlite3_api_routines *api )
{
    SQLITE_EXTENSION_INIT2(api);

    sqlite3_create_function( db, "product", 1, SQLITE_UTF8,
            NULL, NULL, product_step, product_final );
    sqlite3_create_function( db, "product", 2, SQLITE_UTF8,
            NULL, NULL, product_step, product_final );

    return SQLITE_OK;
}
  • 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-05-18T01:46:23+00:00Added an answer on May 18, 2026 at 1:46 am

    In product_step, after your test for st == NULL, try adding this:

    if (0.0 == st->total_data) st->total_data = 1.0;
    

    In general, you should be very wary of comparing a floating point value for equality. Floating point math is limited in its precision. However, a straightforward comparison should work fine here, provided none of the values in the row are zero: the first time through, the buffer returned by sqlite3_aggregate_context has been zeroed by sqlite, so the value should compare equal to 0.0.

    If a row value being zero is possible (and it likely is), you should add state to the struct to tell whether initialization has been done:

    typedef struct product_state_s {
       double   total_data;  /* sum of (data * weight) values */
       double   total_wt;    /* sum of weight values */
       bool     did_init;
    } product_state;
    

    and, later, instead of testing whether st->total_data is zero, instead:

    if (!did_init) {
        st->total_data = 1.0;
        st->did_init = true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that will calculate the minimal area taken by fitting rectangles
As a Christmas gift I have written a small program in Java to calculate
I have a program that spits out both standard error and standard out, and
I have a program that uses the mt19937 random number generator from boost::random. I
I have a program that spits out an Excel workbook in Excel 2003 XML
I have UTC time which is coming from UDP, I have a program to
I have a small C program to calculate hashes (for hash tables). The code
I have a program that looks like the following: double[4][4] startMatrix; double[4][4] inverseMatrix; initialize(startMatrix)
I have a program in Perl that works with probabilities that can occasionally be
I have a program that creates a Windows user account using the NetUserAdd() API

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.