Converting strings to giac expressions

### 25.5.3 Converting strings to giac expressions

##### Using strings as commands.

The expr command lets you use a string as a command.

- expr takes str, a string which expresses a valid command.
- expr(str) converts str to the command and evaluates it.

#### Examples

expr("c:=1"):; c

1 |  |  |  |  |  |  |  |  |  |

a:="ifactor(54)":; expr(a)

2· 33 |  |  |  |  |  |  |  |  |  |

which is the same thing as entering ifactor(54) directly.

##### Converting strings to numbers.

You can also use expr to convert a string to a number. If a string is simply a number enclosed by quotation marks, then expr will return the number.

#### Examples

The following strings will be converted to the appropriate number.

A string consisting of the digits 0-9 which does not start with 0 will be converted to an integer:

expr("2133")

2133 |  |  |  |  |  |  |  |  |  |

A string consisting of the digits 0-9 that contains a single decimal point will be converted to a double:

expr("123.4")

123.4 |  |  |  |  |  |  |  |  |  |

A string consisting of the digits 0-9 and a single decimal point, followed by e and then more digits, will be read as a floating point number:

expr("1.23e4")

12300.0 |  |  |  |  |  |  |  |  |  |

A string consisting of the digits 0-7 which starts with 0 will be read as an integer base 8:

expr("0176")

126 |  |  |  |  |  |  |  |  |  |

since 176 base 8 equals 126 base 10.

A string starting with 0x followed by digits 0-9 and letters A-F (or a-f) will be read as an integer base 16:

expr("0x2a3f")

10815 |  |  |  |  |  |  |  |  |  |

since 2A3F base 16 equals 10815 base 10.

A string starting with 0b followed by digits 0 and 1 will be read as a binary integer:

expr("0b1101")

13 |  |  |  |  |  |  |  |  |  |

since 1101 base 2 equals 13 base 10.