Giter VIP home page Giter VIP logo

Comments (2)

thejoshwolfe avatar thejoshwolfe commented on May 14, 2024

here's a test for trying out hex float literals in C99

#include <stdio.h>
#include <stdint.h>

int main() {
    float f = 0x1f.0fp-4f;
    uint32_t bits = *(int *)&f;
    for (int i = 31; i >= 0; i--) {
        if (i == 30 || i == 22) printf(" ");
        printf("%d", (bits >> i) & 1);
    }
    printf("\n");
    return 0;
}
$ gcc -std=c99 -Wall -Werror -Wpedantic a.c && ./a.out && rm a.out 
0 01111111 11110000111100000000000

from zig.

thejoshwolfe avatar thejoshwolfe commented on May 14, 2024
[<radix>]<whole_number>[.<fraction>][<exponent_designtator>[<sign>]<exponent>]

radix can be 0x for hex, 0o for octal, or 0b for binary. In hex, the digits a-f are case insensitive.

if either the fraction or exponent are given, then it is a float literal. otherwise, it's an integer literal.

the whole_number and the fraction are interpreted in the radix specified. the exponent is always specified in base 10.

exponent_designator is e or E for base 2, 8, and 10, and p or P for base 16.

sign is + or -.

the significance of the exponent is 10^exp for base 10 floats and 2^exp for base 2, 8, and 16 floats. This means that 0x1p4 is 0x10 not 0x10000.

This is very similar to the C99 syntax. It is more flexible in these ways:

  • octal literals start with 0o instead of just 0.
  • binary literals exist.
  • octal and binary float literals exist.
  • the exponent is not required for hex float literals.

I don't know about compliance with the IEEE 754-2008 standard; I can't find a copy of it on the internet.

from zig.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.