Hello.
Based on the official Nim documentation, I can see there's a mismatch between the documentation and the current functionality of the Nim language regarding floating-point literals. Here's the issue and corresponding examples detailed:
According to the official Nim documentation, the language is expected to support the following:
FLOAT32_SUFFIX = ('f' | 'F') ['32']
FLOAT32_LIT = HEX_LIT ''' FLOAT32_SUFFIX | (FLOAT_LIT | DEC_LIT | OCT_LIT | BIN_LIT) ['''] FLOAT32_SUFFIX
This suggests that Nim should be able to interpret floating-point literals (FLOAT32_LIT) that are hexadecimal numbers (HEX_LIT), floating-point numbers (FLOAT_LIT), decimal numbers (DEC_LIT), octal numbers (OCT_LIT), or binary numbers (BIN_LIT), followed by an optional suffix indicating it's a 32-bit floating-point literal (FLOAT32_SUFFIX).
• Floating-point literals in hexadecimal: HEX_LIT cannot be used with FLOAT32_SUFFIX in Nim. This is mentioned in an open issue in the official Nim repository on GitHub nim-lang.org.
• Floating-point literals in octal and binary format: OCT_LIT and BIN_LIT cannot be used with FLOAT32_SUFFIX in Nim.
Therefore, the part of the pattern that doesn't work in Nim is HEX_LIT ''' FLOAT32_SUFFIX, OCT_LIT FLOAT32_SUFFIX, and BIN_LIT FLOAT32_SUFFIX. Only decimal format floating-point literals (FLOAT_LIT and DEC_LIT) can have a FLOAT32_SUFFIX in Nim.
Here are the corresponding examples:
let floatLit = 1.0'f32
let decLit = 123'f32
let hexLit = 0x1.0'f32 # Nim doesn't support floating-point literals in hexadecimal
let octLit = 0o1.0'f32 # Nim doesn't support floating-point literals in octal format
let binLit = 0b1.0'f32 # Nim doesn't support floating-point literals in binary format
It is recommend that these issues be addressed so that Nim's functionality aligns with the official documentation.
Best regards.
Antonio F.S.
You can use hex literals in your code to represent Decimal value 129 in Hex form '0x123', not the other way around: '0x123.0 != 123.0'.
If you want to represent Decimal value in other bases you can use functions defined in 'std/strutils <https://nim-lang.org/docs/strutils.html>'_: toBin(), toHex(), toOct()...'
let decimalFloat = 123.56
let hexString = cast[int](decimal).toHex(64) #cast is required, because toHex() accepts only the int Type
Regarding ChatGPT or other AI tools: please, be aware they're very prone to hallucinate, even more so on niche topics like Nim programming language.
Further Reading:
Hello.
My intention was not to judge anyone, I am simply learning Nim and together with PHIND, I am confirming with my checks in the IDE VS Code that what is in the manual, corresponds to the execution of the sentences.
I use IA because it is very difficult for a novice to decipher without examples, according to what contents of the Nim manual.
I posted the answer thinking that there was a manual error, but seeing that this is not the case, I will rely less on the AI and I will post my doubts here more often.
Apologies and thanks. Antonio F.S.
Thank you Yardanico and janAkali for your explanations but above all, for the examples (in Spain we say that a picture is worth a thousand words :-) ). Now I understand better.
Thank you Araq too, for your explanation.
Best regards. Antonio F.S.