getting int value from IntegerLiteral, float value from FloatingLiteral

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

getting int value from IntegerLiteral, float value from FloatingLiteral

Rajendra
Hi,

I want to get precise int value for a variable from IntegerLiteral type
and float value for a variable from FloatingLiteral type (do not want to
loose precision). I don't see a function to call to get int or unsigned
int directly.

Is following code only correct way or is there any better way to get
these?

     const Expr *e = varDecl->getInit();
     const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(e);  // e is of
type Expr*
     double val = IL->getValue().signedRoundToDouble();
     int ival = (int) val;
     std::cerr << "\tinitial value: " << val << " ival " << ival <<
"\n";

     const Expr *e = varDecl->getInit();
     const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(e);  // e is
of type Expr*
     double val = FL->getValue().convertToDouble();
     float fval = (float) val;
     std::cerr << "\tinitial value: " << val << " fval " << fval <<
"\n";

Please advise.

- Rajendra
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: getting int value from IntegerLiteral, float value from FloatingLiteral

Rajendra
Any suggestions on this please?

-Rajendra
Reply | Threaded
Open this post in threaded view
|

Re: getting int value from IntegerLiteral, float value from FloatingLiteral

João Matos
You can get the value with the llvm::APInt::getLimitedValue method.

On Mon, Dec 17, 2012 at 9:05 AM, Rajendra <[hidden email]> wrote:
Any suggestions on this please?

-Rajendra



--
View this message in context: http://clang-developers.42468.n3.nabble.com/getting-int-value-from-IntegerLiteral-float-value-from-FloatingLiteral-tp4029017p4029036.html
Sent from the Clang Developers mailing list archive at Nabble.com.
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev



--
João Matos

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: getting int value from IntegerLiteral, float value from FloatingLiteral

Chandler Carruth
In reply to this post by Rajendra
On Sat, Dec 15, 2012 at 10:32 PM, Rajendra <[hidden email]> wrote:
Hi,

I want to get precise int value for a variable from IntegerLiteral type and float value for a variable from FloatingLiteral type (do not want to loose precision).

If you do not want to lose precision, you do not want an 'int' or 'float'. The literal AST nodes expose an APInt and APFloat (resp.) which encode the exact value with full precision. See the documentation for their APIs, including methods to query how much precision is required to represent them exactly, and other basic properties.
 
I don't see a function to call to get int or unsigned int directly.

Is following code only correct way or is there any better way to get these?

    const Expr *e = varDecl->getInit();
    const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(e);  // e is of type Expr*
    double val = IL->getValue().signedRoundToDouble();
    int ival = (int) val;
    std::cerr << "\tinitial value: " << val << " ival " << ival << "\n";

    const Expr *e = varDecl->getInit();
    const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(e);  // e is of type Expr*
    double val = FL->getValue().convertToDouble();
    float fval = (float) val;
    std::cerr << "\tinitial value: " << val << " fval " << fval << "\n";

Please advise.

- Rajendra
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev