Sheerpower Logo

S.1  Advanced REAL and String Data Types


Advanced Tutorial: Understanding SheerPower REALs

The SheerPower REAL data type is not like typical floating-point numbers used in other programming languages. These languages rely on approximate binary-based representations. Instead, SheerPower ensures perfect precision for all decimal values by using an exact internal format.

1. REAL: Internal Representation

SheerPower stores every REAL number using two 64-bit signed integers:

  • IP – The integer part (digits before the decimal)
  • FP – The fractional part (digits after the decimal), scaled by 10^16
value = 13.599 IP = 13 FP = 5990000000000000

This separation allows SheerPower to maintain complete decimal accuracy with up to 18 digits before and 16 digits after the decimal point.

2. Visualizing the Structure

The illustration below shows how a REAL value is stored:

REAL = 13.599

A REAL uses two seperate 64-bit integers.
IP (Integer Part)
13
FP (Fractional Part)
5990000000000000

3. Perfect Precision in Action

Here's a classic example of floating-point failure in other languages:

print (0.1 + 0.2) - 0.3

In C, JavaScript, Python, or Java, this returns:

0.0000000000000000555...

But in SheerPower, the output is:

0

SheerPower gives you exact answers — no need for "close enough" comparisons or fuzzy tolerances.

4. Why This Matters

  1. Accurate accounting — no rounding errors over time
  2. Reliable financial systems — every penny is preserved
  3. Clean scientific and statistical results
  4. No surprises when comparing REALs — exact match works

5. Summary

SheerPower’s REAL type guarantees perfect decimal precision by storing values using two separate 64-bit integers. This removes all floating-point rounding issues and makes SheerPower the ideal choice for business, financial, and scientific applications where accuracy matters.

String Attributes for Efficient Processing

In this advanced discussion on string handling, we delve deeper into SheerPower's memory management techniques and optimization strategies for handling strings efficiently.

  1. Length-Aware Strings
    SheerPower STRING variables store their length internally, allowing immediate length access without scanning for a terminator.
  2. Efficient Memory Management
    STRINGs also remember how much memory has been allocated. This allows appending and resizing operations to run quickly without reallocation.
  3. Binary-Safe Content
    Since the length is tracked internally, a STRING can include binary data, including embedded null characters.
    name$ = "A" + char(0) + "B"
  4. External Compatibility
    When passed to routines written in C or other languages, SheerPower automatically appends a zero terminator to the STRING for compatibility.
    call "c_function" (name$) ! name$ is zero-terminated on output

Memory Buffer Reuse

SheerPower manages strings by reusing memory buffers, which significantly reduces the overhead of memory allocations and deallocations. For example, if a string holds the words "The rain in Spain" and later needs to be shortened to "Enjoy life", SheerPower can repurpose the existing buffer to store the shorter string. In most applications over 99% of string buffers are reused.

This approach avoids the frequent memory operations that can cause performance bottlenecks in other languages.

Comparison with Other Languages

In many programming languages, every string assignment or modification requires a new memory allocation, and the previously used memory must be deallocated—either immediately or by a garbage collector. This repetitive allocation-deallocation cycle introduces overhead that can slow down applications, especially in loops or large-scale data processing.

Consider the following code, where a new string is created and assigned repeatedly in a loop:

for i = 1 to 10_000
num$ = str$(i)
next i
Note: In SheerPower, only one memory allocation is needed for num$ at the beginning of the loop, thanks to its string reuse mechanism. Throughout the loop, the memory allocated for num$ is reused, and no additional allocations or deallocations are required. This is a stark contrast to other languages, where this code could lead to as many as 10,000 allocations and deallocations, slowing down performance.

How SheerPower Outperforms Common Languages in String Management

  1. Python: Each iteration creates a new string object, resulting in 10,000 allocations and triggering garbage collection cycles.
  2. PHP: Similar to Python, PHP requires a new allocation for each string assignment, with old strings marked for garbage collection.
  3. Go: Although Go is efficient, it still performs an allocation for each new string in the loop and relies on its garbage collector for deallocation.
  4. Rust: Rust deterministically deallocates memory at the end of each loop iteration, but this still results in 10,000 allocations and deallocations for the code above.

In contrast, SheerPower’s single allocation and zero deallocation approach makes it uniquely suited for high-performance applications that require repetitive data processing. This performance advantage is not just a small optimization—it’s a fundamental design feature that allows SheerPower to outperform other languages in memory efficiency and execution speed.

Ideal Use Cases for SheerPower’s Memory Efficiency

SheerPower's memory management shines in applications where performance is critical, such as:

  • High-frequency trading platforms
  • Real-time data analysis
  • Financial applications with large volumes of calculations
  • Batch processing systems that require rapid string or data transformations

By reducing the memory management overhead typically seen in high-level languages, SheerPower offers developers the power of low-level memory efficiency with the simplicity and productivity of a high-level language.

Globally Unique String IDs (SIDs)

Each string in SheerPower is assigned a Globally Unique String ID (SID). When a string is copied, the SID is also copied to the target. This allows SheerPower to check the SIDs of the source and target strings before performing any copy operation. If the SIDs match, the SheerPower avoids unnecessary string copying, which optimizes performance, especially in applications with frequent string manipulations.

Hinting System

Functions like getword$() and geodistance() leverage SheerPower's SID system as part of an internal hinting mechanism. This hinting system enhances efficiency by remembering the last position accessed in a string and last string to number conversions. For example, if you have stored the entire contents of the Bible in a variable bible$ and use getword$(bible$, 1000) to retrieve the 1000th word, and l ater use getword$(bible$, 1001), SheerPower doesn't need to re-read the first 1000 words. Instead, it uses the hinting system to quickly locate and return the 1001st word.

Note: SheerPower provides memory safety without relying on a garbage collector. Many modern programming languages use a garbage collector to automatically manage memory, but this can cause pauses during program execution. In contrast, SheerPower’s approach to memory safety avoids these interruptions entirely by using multiple memory pools and extensive buffer reuse.

By understanding these advanced techniques, you can fully harness SheerPower's capabilities to build highly efficient, scalable, and robust business applications that handle strings with exceptional performance.

Hide Description

    

       


      

Enter or modify the code below, and then click on RUN

Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use.