AcademySoftwareFoundation / AcademySoftwareFoundation/MaterialX
Default float formatting causes programmatically generated documents to be invalid
- Dominant language
- C++
- Stars
- 2.3k
- Forks
- 451
- Avg merge
- 6d 6h
- Merged PRs (30d)
- 5
Description
When using the C++ API with default settings, very small or large float values (f.i. from an external data source) can cause a programmatically generated document to be invalid.
Following test case demonstrates the issue:
in _MaterialXTest/MaterialXFormat/XmlIo.cpp_:
```cpp
TEST_CASE("Float value representation", "[xmlio]")
{
float floatCloseToZero = std::nextafter(0.0f, 1.0f);
auto floatString = mx::Value::createValue(floatCloseToZero)->getValueString();
mx::DocumentPtr doc = mx::createDocument();
auto node = doc->addNode("constant", mx::EMPTY_STRING, "float");
auto input = node->addInput("value", "float");
input->setValueString(floatString);
std::string errMsg;
bool validationResult = doc->validate(&errMsg);
if (!errMsg.empty())
{
fprintf(stderr, "%s\n", errMsg.c_str());
}
REQUIRE(validationResult);
}
```
The problem is that the \ API is used to serialize the float to a string (Value.cpp:95), where the default format mode emits a scientific notation (`1.4013e-45` in this case), which is not valid MaterialX float syntax. The behaviour can be 'fixed' by setting the float format mode:
```cpp
mx::ScopedFloatFormatting fmt(mx::Value::FloatFormatFixed);
```
However, this is not the default behaviour and the need for setting a specific float format mode only becomes apparent after running into the problem described above.
Contributor guide
Research direction
Start with the supplied Float value representation test in _MaterialXTest/MaterialXFormat/XmlIo.cpp_ and inspect the float serialization at Value.cpp:95. Run the test to reproduce the invalid document; done means the default settings produce valid MaterialX float syntax and the validation assertion passes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- computer-graphics
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 42/100