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.

absolute value in fixed-point

Status
Not open for further replies.

Elnaz

Newbie level 5
Joined
Mar 22, 2007
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,346
Hi all,

In order to calculate the absolute value of a number in fixed-point, I'm doing num = num & ~(1<<BP) where num is a 64 bit signed fixed-point value (32 integer bits plus 32 fractional bits) and BP is the wordlength=64. But running the C code in VS 2010 I end up getting the same negative number. Theoritically it should work, right? but, it doesn't!


Thanks
Elnaz
 

I presume, you are working with regular 2s complement numbers. So a bitwise and can hardly be the right operation to calculate an absolute value. Absolute value means negating the number when it's negative. Negation is done by inverting all bits (1s complement) and adding 1.
 
  • Like
Reactions: Elnaz

    Elnaz

    Points: 2
    Helpful Answer Positive Rating
I presume, you are working with regular 2s complement numbers. So a bitwise and can hardly be the right operation to calculate an absolute value. Absolute value means negating the number when it's negative. Negation is done by inverting all bits (1s complement) and adding 1.

Thanks FvM. You're absolutely right.
I have another question as well. I don't know why I'm getting a "corruption of the heap" error when I "free" the menory of a fixed-point variable? I was always doing this after I'm done with a floating point variable but with the fixed-point it doesn't work. The "delete" function doesn't work either. The code is in C in Visual Studio.

Thanks,
Elnaz
 

Heap operation doesn't care for the meaning of the data, if they are floating point or fixed pont. The important point is to strictly observe the size of entities allocated with new() or other methods. Heap corruption usually happens by writing "out of bounds" or using a variable after freeing it.

You can also ask, if dynamic memory usage serves a reasonable purpose in your application, e.g. because you are dealing with huge structures. In most cases, you don't need to care even for a few thousand statically allocated variables.
 

Right, but how can I use dynamic memory in case of fixed-point? With floating-type I was using malloc. Is there a way to do the same with fixed-type?

Elnaz
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top