Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Converting float to longword

Status
Not open for further replies.

erio

Full Member level 3
Joined
Feb 26, 2008
Messages
175
Helped
27
Reputation
54
Reaction score
23
Trophy points
1,298
Location
Indonesia
Activity points
2,168
Hello...first, sorry for my bad english, i hope you are understand what i mean.
i`m doing some project with lots of float data type. now i need to convert the result of calculation(which is a float data type) to longword so i can use the DIV or MOD in mikrobasic PRO 3.80. anyone can help me?

MCU = PIC18F2550 @20MHz

best regards

Erio
 

anyone can help me?

no body can help you because nobody knows how you intend to convert a float to long (?). people may be able to help you with the conversion itself but you have to decide how you want to convert it.
 
  • Like
Reactions: erio

    erio

    Points: 2
    Helpful Answer Positive Rating
If you have a float f and a long variable l, you can simply write l = f, and the implicite standard C conversion will assign the integer part of f to l. That's the simple answer to your question in terms of the C language standard.

If you intend something different, like a fixed point representation of a float value, you have to define the respective numeric format first. You basically have to specify the number of integer and fractional bits.
 
  • Like
Reactions: erio

    erio

    Points: 2
    Helpful Answer Positive Rating
Thank you for the answer guys..to make things clearer..suppose i have a float f=98765.6988. Now i just want to convert it to longword with rounding to nearest integer (so the result of example above will be 98766)..thank you for your attention

best regards

erio
 

just think about it: FvM has provided an answer to you that is accurate if your float type f has no decimal points. aka FvM's approach would be correct if f is between 98765.0 - 98765.4.

that approach would be wrong if f is [98764.5 - 98764.9].

essentially, you want a conversion that pushes a number between 98764.5 - 98765.4 (that number, per your desire, will convert to 98765) to above 98765.0 but less than 98765.9 for FvM's approach to be correct.

there are simply too many ways to do that, with one particular way being extremely efficient.
 
  • Like
Reactions: erio

    erio

    Points: 2
    Helpful Answer Positive Rating
The exact behaviour of the implicite C type conversion or (long) type cast isn't specified by the C standard as far as I know. Particularly the IEC spec opens this point for implementation dependance. If a certain implementation is simply cutting the fractional part, it may be different with another.

If you want a clear behaviour, use round(), floor() or ceil() - whatever you want.
 
  • Like
Reactions: erio

    erio

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top