Skip to content

fix(scripting): do arithmetic on numeric strings and join numbers with .. - #44

Closed
L4co77 wants to merge 1 commit into
main-picknikfrom
fix/24179-script-untyped-arithmetic
Closed

L4co77 wants to merge 1 commit into
main-picknikfrom
fix/24179-script-untyped-arithmetic

Conversation

@L4co77

@L4co77 L4co77 commented Nov 2, 2026

Copy link
Copy Markdown

[written by AI]

Refs PickNikRobotics/moveit_pro#24179 (story A of PickNikRobotics/moveit_pro#18694)

Motivation

Numbers often reach Script expressions as strings, for example through SubTree ports. Today -, *, / on them fail with Operation not permitted, '3 apples' + 4 silently gives 7 because Any::stringToNumber accepts a numeric prefix, 1 .. 2 returns an empty value that := reports as "not initialized", and .. prints doubles as 2.000000. The measured table is on PickNikRobotics/moveit_pro#18694.

Brief description

All in include/behaviortree_cpp/scripting/operators.hpp. Helpers live in BT::Ast::detail; Any::stringToNumber and Any's double-to-string conversion are unchanged, so port conversions elsewhere in the library behave as before.

  • A string is a number in a script only if all of it, apart from surrounding whitespace, is a finite decimal number. ' 5 ' is 5; '3 apples', '5000ms', '+5', '0x10', 'nan', 'inf', '1e999', '1e-400' and '' are not. A character check runs before parsing so every standard library rejects the same strings.
  • Arithmetic, bitwise (exact int64, also for strings above 2^53), logic, unary - ~ !, compound assignment, ?: and comparisons use that rule. A non-number is a RuntimeError naming the operand; with + it suggests ...
  • + on two strings still concatenates. PickNikRobotics/moveit_pro#24181 removes that in 11.0.0.
  • Two numeric strings compare as numbers, exactly: both integers as int64, otherwise as long double so IsSame applies no tolerance. Numbers too large to compare exactly are an error; strings compare as text only when one of them is not a number.
  • Integer text beyond ±2^53 is an error in arithmetic, as an int64 entry already is, instead of rounding silently. Every number-to-double cast in the operators turns a precision-loss std::runtime_error into a RuntimeError, so ParseScriptAndExecute returns an error instead of throwing.
  • .. joins any strings and numbers. Whole doubles up to 2^53 print as integers (2, not 2.000000); others in the shortest %g form that reads back exactly (0.25, 0.0001, 0.30000000000000004), the same on every standard library.
  • Compound assignment on an entry holding a string keeps a string (c := '1'; c += 1 gives '2'); with a number entry, a numeric string on the right is a number (n := 1; n += '2' gives 3).

Behavior changes to decide on before this ships

compatibility-bot (moveit_pro reviewer role) rated these Needs-discussion for a non-major release. None of the scripts shipped in moveit_pro or moveit_pro_example_ws change outcome; the only visible difference is one log line in hangar_sim/objectives/calibrate_sam3_mask_areas.xml (threshold=0.250000 → threshold=0.25).

  1. Two numeric strings compare as numbers: '10' > '9' becomes true; '007' == '7' and '1.0' == '1' become equal. Previously text comparison.
  2. Strings that only start with a number now stop the Objective with an error instead of being read by their leading digits: '5000ms' / 1000 (was 5), '3 apples' == 3 (was true), '3 apples' ? 1 : 2 (was 1), x = '3 apples' into a number entry (was 3), '0x10' == 0 (was true), 'inf' > 1 (was true).
  3. .. drops trailing zeros on doubles.

Options for 1 and 2: accept in the next minor with a Breaking Change: release note in the moveit_pro BTCPP_PICKNIK_VERSION bump PR, or hold those parts for 11.0.0 with #24181.

How it was tested

Built in a container from the moveit-pro-dev image (clang with libstdc++ 13), plain CMake, BTCPP_GROOT_INTERFACE=OFF.

  • Full suite: 583/583 (580 on main-picknik before the change, plus ParserTest.UntypedNumericOperands, ParserTest.NumberStreamFallbacks and SubTree.ScriptingTypingIssue_SubtractNumericStringPort).
  • The new tests fail against the old operators.hpp.
  • ParserTest.* and SubTree.* clean under ASan and UBSan.
  • pre-commit (clang-format 17, clang-tidy, codespell) clean on the changed files.
  • Not exercised: the branches for standard libraries without floating-point from_chars (libc++). ParserTest.NumberStreamFallbacks calls the stream parse and format functions directly; the from_chars vs. stream choice inside ParseWholeNumber only runs there.

Follow-ups after merge

  • apt_build_farm: git_sha and revision 6 → ros-jazzy-behaviortree-cpp-picknik=4.9.0-6noble.
  • moveit_pro: bump BTCPP_PICKNIK_VERSION, add the script operator reference page, correct the two operator sentences in PickNikRobotics/moveit_pro#24177's docs and the pin line in docs_nonpublic/design/behaviortree-cpp-fork-coexistence.md, and add the release notes.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants