-3

How is floating point represented in assembly for pic16?

For example, if I want to save a value of 1.623 in memory, how can I save it using the 36 assembly instructions in pic16?

I don't want to use a rounding technique, I need to save in memory exactly floating point for example 1.623.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
rami
  • 1
  • [FAQ on how to question on stackoverflow](https://stackoverflow.com/help/asking) – Morse Apr 23 '18 at 16:17
  • any way you wish? If you are ok with aggressive rounding, you can store it as integer value `1`. If you are not ok with aggressive rounding, you forgot to explain that in the question and put there some parameters what you need. Of course the computer has finite amount of memory and resources, so you can't encode every number into it, as the amount of numbers is infinite. http://floating-point-gui.de/ – Ped7g Apr 23 '18 at 16:34
  • I don't wanna use rounding technique, I need to save in a memory exactly floating point for example 1.623, this is why I didn't mention rounding technique. As you know when you write the value of 1.623 using C-language, the value is converted to assembly language through some instructions in assembly, so, I wanna know those instructions in assembly language. thank you – rami Apr 23 '18 at 19:06
  • 1
    If you never want any rounding, you can't use floating point. You could use something like arbitrary-precision rational numbers, as long as you don't need any transcendental functions (sin/cos/tan, log/exp, or whatever). But that uses an unbounded amount of storage for each number. – Peter Cordes Apr 24 '18 at 01:28
  • @rami when you write value `float x = 1.623;` in C language, it is being rounded to nearest possible `float` type value, i.e. in binary `00111111110011111011111001110111` and that is exactly `1.62300002574920654296875`, **not** `1.623`. So you missed the whole point of the http://floating-point-gui.de/ link I posted in my first comment. Not sure how to interpret that, looks like you are either too lazy to read+study, or too dumb for programming, maybe both. ... *"as you know"*... ridiculous. – Ped7g Apr 24 '18 at 06:27

2 Answers2

3

I think you don't know enough about floating point to know that your requirements don't make sense. But let's take it at face value and see what's possible.

I don't wanna use rounding technique, I need to save in a memory exactly floating point for example 1.623

If you never want any rounding, you can't use floating point. You could use something like arbitrary-precision rational numbers, as long as you don't need any transcendental functions (sin/cos/tan, log/exp, or whatever). i.e. represent your numbers as an infinite-precision numerator and infinite-precision denominator.

But that uses an unbounded amount of storage for each number; you can normalize by cancelling common factors after operations, but after many operations there's no limit to how large it grows. This is probably not what you want for PIC.


But maybe you're ok with rounding the result of 10. / 3. (an infinite repeating fraction starting with 3.33333...) to something that can be stored in a fixed 4 bytes.

If you don't want any rounding for decimal constants with only a few significant figures like 1.623, you could use decimal floating point where the exponent field represents a power of 10, so you can represent short decimal fractions like 1.623 exactly. Since PIC doesn't have hardware FP, you'd have to implement it in software anyway.

The mantissa can stored as a binary integer (or BCD or whatever you like), but with limited range: it's only allowed to be in the range [0, 10P-1).

IEEE754-2008 does define some standard decimal-FP formats like decimal32; some CPUs like IBM POWER6 and later actually implement it in hardware as well as normal binary floating point.


Normal binary floating point:

Mainstream CPUs with FPUs (and most software FP implementations) typically use IEEE binary floating point, where numbers are represented as m * 2^e (where m is the mantissa, aka significant, in the range [1.0 , 2.0), represented with an implicit leading 1.) Like https://en.wikipedia.org/wiki/Single-precision_floating-point_format. This format can't exactly represent 1.623. Try it for example on this online IEEE754 converter

The closest representable single-precision float is 1.62300002574920654296875, represented by a bit-pattern of 0x3fcfbe77. That's what a C compiler will store in memory when you compiler float foo = 1.623; (assuming the C implementation uses IEEE binary32 for its float; this is not required by ISO C11).


Fixed point:

You may not want floating point at all: without a hardware FPU, fixed-point is often a more efficient way to represent fractional numbers.

A binary fixed-point system can't represent 1.623 exactly, though, but a decimal-based one can. (Where the value represented by the signed 2's complement integer i is i / 10000 for example. So you can represent values from -3.2768 to +3.2767 with that format.)

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
2

How is floating point represented in assembly for pic16?

It is a question of what compiler you use and how much code you want to write as the PIC 16 does not contain FP instructions.


Various PIC compilers employ float in nearly binary32. Often the compiler does not quite handle sub-normal numbers, not-a-numbers and the rounding modes 100% correctly. For many applications, this non-compliance is not important.

A PIC complier may use a different float format: 8-bit exponent, sign, 23-bit mantissa - in that order


if I want to save a value of 1.623 in memory, how can I save it using the 36 assembly instructions in pic16?

It sounds like you have requirements that need an exact decimal format. Such a format exist call decimal32. Although this format well meets your goals, it requires a fair amount of coding to support it as it is unlikely your PIC compiler does that already.

You may want to reconsider the coding goals, perhaps using fix-point formats.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256