为float变量赋值会更改C中的值

时间:2018-01-04 20:49:24

标签: c floating-point

我正在尝试计算整数的平均值并将其分配给float变量。当我用cgdb调试它并打印平均计算的右侧时,它给出了正确的数字。但是,当我将其分配给(float*)payload时,值会从401850471更改为401850464.00

float sum= 0.0;

for (int i = 0;
     i<avg_operator->data_source->column_pointer.result->num_tuples; 
     i++) {
     sum+= ((int*)avg_operator->data_source->column_pointer.result->payload)[i];     
}

((float*)avg_operator->result->payload)[0]=
sum/(float)avg_operator->data_source->column_pointer.result->num_tuples;

3 个答案:

答案 0 :(得分:2)

你不能通过强制转换指针来将int转换为浮点数,这会给出一个随机/未定义的值。您需要取消引用浮点指针,并分配

答案 1 :(得分:1)

的是:

/**
 * Set all excluded items for sitemap in a transient
 *
 * @return mixed
 */
function beee_get_sitemap_excludes_transient() {

    // get transient
    $output = get_transient( 'beee_exclude_sitemap' );

    // if transient returns false, create it
    if ( false == $output ) {

        $exclude_args = array(
            'post_type'      => [ 'page' ], // change this to the post types you want excluded
            'posts_per_page' => -1, // all items of course
            'meta_query'     => array(
                array(
                    'key'     => 'beee_exclude_sitemap',
                    'value'   => '1',
                    'compare' => '=',
                ),
            ),
            'orderby'        => 'ID',  // recommend to set to ID so 'the order is easier to compare later on', otherwise it can create issues
            'order'          => 'ASC', // same, just sort ASC to avoid issues
        );
        $excluded_items = get_posts( $exclude_args );
        if ( count( $excluded_items ) > 0 ) {
            // if there are items and create an empty array
            $exclude = [];
            foreach( $excluded_items as $item ) {
                // add post id to array
                $exclude[] = $item->ID;
            }
            // create a string from an array since Yoast stores a string, not an array
            $output = implode( ',', $exclude );

            // set transient with a 24-hour expiration
            set_transient( 'beee_exclude_sitemap', $output, 24 * HOUR_IN_SECONDS );
        }
    }

    return $output;
}

/**
 * Set Yoast settings from transient
 */
function beee_exclude_pages_sitemap( $post_id ) {

    // get our excluded items from transient
    $our_excluded_posts = beee_get_sitemap_excludes_transient();

    // check if 'exclude' field has been set (in the post)
    if ( 1 == get_field( 'beee_exclude_sitemap', $post_id ) ) {
        // if yes, check if it exists in transient
        if ( in_array( $post_id, explode( ',', $our_excluded_posts ) ) ) {
            // yes, it's already included so we don't have to do anything
        } else {
            // no, it's not included so it needs to be added
            // so we delete the transient (existing values)
            delete_transient( 'beee_exclude_sitemap' );
        }
    } else {
        // it has not been set in the post
        if ( in_array( $post_id, explode( ',', $our_excluded_posts ) ) ) {
            // if does exists in our stored transient, which it shouldn't be in there
            // so we delete the transient (existing values)
            delete_transient( 'beee_exclude_sitemap' );
        }
    }

    // get our excluded ids from transient
    // since we just cleared it, the transient doesn't exist and is retrieved from scratch
    $our_excluded_posts = beee_get_sitemap_excludes_transient();

    // get post ids from Yoast settings
    $wpseo_xml = get_option( 'wpseo_xml' );

    // compare Yoast's value with our items
    if ( $wpseo_xml[ 'excluded-posts' ] == $our_excluded_posts ) {
        // they are the same so we don't need to do anything
    } else {
        // they are not the same so we need to update the database with our new ids
        update_option( 'wpseo_xml', $wpseo_xml );
    }
}
add_action( 'save_post', 'beee_exclude_pages_sitemap' );

不是正在投射,而是对编译器撒谎。您应该取消引用,并且不需要转换为浮动,因为转换为整数是自动完成的:

((float*)avg_operator->result->payload)[0]= sum/(float)avg_operator->data_source->column_pointer.result->num_tuples;

(好吧,也许你需要对值进行舍入而不是截断)

另外,因为avg_operator->result->payload[0]= sum/(float)avg_operator->data_source->column_pointer.result->num_tuples; 是一个整数,所以也不需要转换为整数指针,只需这样做:

payload

并将sum+= avg_operator->data_source->column_pointer.result->payload[i]; 定义为浮点数,人们永远不会知道浮点累积错误(如果总和对于整数来说不是太大,那就是)

答案 2 :(得分:0)

  

当我用cgdb调试它并打印平均计算的右侧时,它给出了正确的数字。

调试器使用double数学显示商。 C允许float分区使用更广泛的类型。但是,一旦将商分配给float,就可能出现精度缩小。

401850471是29位值。 float通常具有24位精度。必须有所作为。

401850464.0是最接近可表示的float到401850471,所以说得好,至少有一个合理的结果。

OP还在做其他奇怪的代码操作。推荐的解决方案以更宽sum类型和更精确的划分和存储开始。

long long sum = 0.0;

int n = avg_operator->data_source->column_pointer.result->num_tuples
int *data = (int*)avg_operator->data_source->column_pointer.result->payload;
for (int i = 0; i < n; i++) {
  sum += data[i];
}

double average = 1.0 * sum / n;
printf("Average %f\n", average);

如果答案必须是float,则代码必须采用四舍五入(二元意义上的)平均值。