Click here to Skip to main content
15,887,816 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Netbeans C++ Sqlite3 Pin
Richard MacCutchan16-Feb-10 6:00
mveRichard MacCutchan16-Feb-10 6:00 
GeneralRe: Netbeans C++ Sqlite3 Pin
loyal ginger16-Feb-10 6:39
loyal ginger16-Feb-10 6:39 
AnswerRe: Netbeans C++ Sqlite3 Pin
sashoalm16-Feb-10 5:06
sashoalm16-Feb-10 5:06 
GeneralRe: Netbeans C++ Sqlite3 Pin
Richard MacCutchan16-Feb-10 8:10
mveRichard MacCutchan16-Feb-10 8:10 
AnswerRe: Netbeans C++ Sqlite3 Pin
CPallini16-Feb-10 11:15
mveCPallini16-Feb-10 11:15 
GeneralRe: Netbeans C++ Sqlite3 Pin
xivShin16-Feb-10 18:44
xivShin16-Feb-10 18:44 
GeneralRe: Netbeans C++ Sqlite3 Pin
CPallini16-Feb-10 20:53
mveCPallini16-Feb-10 20:53 
QuestionStatic Const Initialization Pin
Skippums16-Feb-10 3:17
Skippums16-Feb-10 3:17 
I am getting unexpected results due to my lack of understanding of static const initialization. Basically, I thought the rule is that static const members are initialized in the order in which they are declared (just like member variables). However, I have some code that is yielding unexpected results, which I have copied below to illustrate the issue.
// Common.h
#pragma once
#ifndef COMMON_H
#define COMMON_H

typedef unsigned __int8  __uint8 ;
typedef unsigned __int16 __uint16;
typedef unsigned __int32 __uint32;
typedef unsigned __int64 __uint64;

// Template structures used to get information about IEEE floating-point formats
template<size_t bytes> struct __float {
public:
    // Mantissa size, in bits
    static const __uint8  MantissaSize;
    // Significand size, in bits
    static const __uint8  SignificandSize;
    // Exponent size, in bits
    static const __uint8  ExponentSize;
    // The maximum representable exponent (reserved for inf and NaN)
    static const __uint16 ExponentMax;
    // The exponent bias
    static const __uint16 ExponentBias;
    // An integer with only the bit left of the mantissa set
    static const __int64 SignificandBit;
    // An integer with all bits included in the mantissa set
    static const __int64 MantissaMask;
    static const __int64 SignificandMask;
    // An integer with all bits included in the exponent set
    static const __int64 ExponentMask;
};
    
// Defines the constants used by the __float structure
template<> const __uint8 __float< 2>::MantissaSize =  10;
template<> const __uint8 __float< 4>::MantissaSize =  23;
template<> const __uint8 __float< 8>::MantissaSize =  52;
    
// Compute the constants for the __float structure
template<size_t bytes>
const __uint8  __float<bytes>::SignificandSize = __float<bytes>::MantissaSize + 1;
template<size_t bytes>
const __uint8  __float<bytes>::ExponentSize    = (bytes << 3) - __float<bytes>::SignificandSize;
template<size_t bytes>
const __uint16 __float<bytes>::ExponentMax     = (1 << __float<bytes>::ExponentSize) - 1;
template<size_t bytes>
const __uint16 __float<bytes>::ExponentBias    = __float<bytes>::ExponentMax >> 1;
template<size_t bytes>
const __int64 __float<bytes>::SignificandBit  =
    static_cast<__int64>(1) << __float<bytes>::MantissaSize;
template<size_t bytes>
const __int64 __float<bytes>::MantissaMask    =
    __float<bytes>::SignificandBit - 1;
template<size_t bytes>
const __int64 __float<bytes>::SignificandMask =
    __float<bytes>::SignificandBit | __float<bytes>::MantissaMask;
template<size_t bytes>
const __int64 __float<bytes>::ExponentMask    =
    static_cast<__int64>(__float<bytes>::ExponentMax) <<
    __float<bytes>::MantissaSize;
    
#endif
And now, the main function:
// Main.cpp
#include <iostream>
#include "Common.h"

using namespace std;

double getOnePointFive() {
    __uint64 rval(0);
    rval = (static_cast<__uint64>(__float<8>::ExponentBias) << __float<8>::MantissaSize) |
           (1ULL << (__float<8>::MantissaSize - 1));
    return *reinterpret_cast<double*>(&rval);
}
    
int main(int argc, const char* argv[]) {
    cout << getOnePointFive() << endl;
    cout << __float<8>::ExponentBias << endl;
    cin.get();
    return 0;
}
When this runs, I would expect to get the result 1.5, and the value of __float<8>::ExponentBias should be 0x3FF. However, I am getting zero for the ExponentBias value which results in the subnormal value 3*2^-1024. Why is the value of ExponentBias coming out as zero? Thanks,
Sounds like somebody's got a case of the Mondays

-Jeff

AnswerRe: Static Const Initialization Pin
«_Superman_»16-Feb-10 3:32
professional«_Superman_»16-Feb-10 3:32 
GeneralRe: Static Const Initialization Pin
Skippums16-Feb-10 3:36
Skippums16-Feb-10 3:36 
AnswerRe: Static Const Initialization Pin
Skippums16-Feb-10 4:37
Skippums16-Feb-10 4:37 
QuestionMFC Dragging Image Pin
RS.Ratheesh16-Feb-10 0:04
RS.Ratheesh16-Feb-10 0:04 
QuestionCom With multiple reference conflict Pin
Ash_VCPP15-Feb-10 23:32
Ash_VCPP15-Feb-10 23:32 
QuestionRe: Com With multiple reference conflict Pin
CPallini16-Feb-10 0:07
mveCPallini16-Feb-10 0:07 
AnswerRe: Com With multiple reference conflict Pin
Ash_VCPP16-Feb-10 1:05
Ash_VCPP16-Feb-10 1:05 
QuestionRe: Com With multiple reference conflict Pin
CPallini16-Feb-10 1:19
mveCPallini16-Feb-10 1:19 
AnswerRe: Com With multiple reference conflict Pin
Ash_VCPP16-Feb-10 1:37
Ash_VCPP16-Feb-10 1:37 
GeneralRe: Com With multiple reference conflict Pin
CPallini16-Feb-10 20:55
mveCPallini16-Feb-10 20:55 
QuestionChanging exe icon at runtime Pin
learningvisualc15-Feb-10 23:01
learningvisualc15-Feb-10 23:01 
AnswerRe: Changing exe icon at runtime Pin
Code-o-mat15-Feb-10 23:22
Code-o-mat15-Feb-10 23:22 
AnswerRe: Changing exe icon at runtime Pin
Saurabh.Garg16-Feb-10 1:01
Saurabh.Garg16-Feb-10 1:01 
QuestionInsert data of more than 4000 characters in clob datatype Pin
MsmVc15-Feb-10 21:06
MsmVc15-Feb-10 21:06 
QuestionRe: Insert data of more than 4000 characters in clob datatype Pin
David Crow16-Feb-10 3:57
David Crow16-Feb-10 3:57 
AnswerRe: Insert data of more than 4000 characters in clob datatype Pin
MsmVc16-Feb-10 19:26
MsmVc16-Feb-10 19:26 
GeneralRe: Insert data of more than 4000 characters in clob datatype Pin
David Crow17-Feb-10 3:06
David Crow17-Feb-10 3:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.