Skip to content

Commit

Permalink
feat: add toFloat methods for Int and String (#1018)
Browse files Browse the repository at this point in the history
### Summary of Changes

* Add `Int.toFloat` and `String.toFloat`
* Update the documentation for the standard library
* Add an NPM script to do so more easily in the future
  • Loading branch information
lars-reimann authored Apr 9, 2024
1 parent ffae98a commit 55a2050
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 25 deletions.
2 changes: 1 addition & 1 deletion docs/stdlib/safeds/data/tabular/containers/Column.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ A column is a named collection of values.
??? quote "Source code in `column.sdsstub`"

```sds linenums="12"
class Column<T = Any?>(
class Column<out T = Any?>(
name: String,
data: List<T> = []
) {
Expand Down
29 changes: 27 additions & 2 deletions docs/stdlib/safeds/lang/Float.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,31 @@ A floating-point number.

??? quote "Source code in `coreClasses.sdsstub`"

```sds linenums="39"
class Float sub Number
```sds linenums="46"
class Float sub Number {
/**
* Converts this floating-point number to an integer by truncating the fractional part.
*/
@Pure
@PythonCall("int($this)")
fun toInt() -> i: Int
}
```

## `#!sds fun` toInt {#safeds.lang.Float.toInt data-toc-label='toInt'}

Converts this floating-point number to an integer by truncating the fractional part.

**Results:**

| Name | Type | Description |
|------|------|-------------|
| `i` | [`Int`][safeds.lang.Int] | - |

??? quote "Source code in `coreClasses.sdsstub`"

```sds linenums="50"
@Pure
@PythonCall("int($this)")
fun toInt() -> i: Int
```
27 changes: 26 additions & 1 deletion docs/stdlib/safeds/lang/Int.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,30 @@ An integer.
??? quote "Source code in `coreClasses.sdsstub`"

```sds linenums="34"
class Int sub Number
class Int sub Number {
/**
* Converts this integer to a floating-point number.
*/
@Pure
@PythonCall("float($this)")
fun toFloat() -> f: Float
}
```

## `#!sds fun` toFloat {#safeds.lang.Int.toFloat data-toc-label='toFloat'}

Converts this integer to a floating-point number.

**Results:**

| Name | Type | Description |
|------|------|-------------|
| `f` | [`Float`][safeds.lang.Float] | - |

??? quote "Source code in `coreClasses.sdsstub`"

```sds linenums="38"
@Pure
@PythonCall("float($this)")
fun toFloat() -> f: Float
```
4 changes: 2 additions & 2 deletions docs/stdlib/safeds/lang/List.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ A list of elements.

??? quote "Source code in `coreClasses.sdsstub`"

```sds linenums="44"
```sds linenums="58"
class List<out E> {

/**
Expand All @@ -34,7 +34,7 @@ Returns the number of elements in the list.

??? quote "Source code in `coreClasses.sdsstub`"

```sds linenums="49"
```sds linenums="63"
@Pure
@PythonCall("len($this)")
fun size() -> size: Int
Expand Down
8 changes: 4 additions & 4 deletions docs/stdlib/safeds/lang/Map.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ A map of keys to values.

??? quote "Source code in `coreClasses.sdsstub`"

```sds linenums="57"
```sds linenums="71"
class Map<K, out V> {

/**
Expand Down Expand Up @@ -49,7 +49,7 @@ Returns the keys of the map.

??? quote "Source code in `coreClasses.sdsstub`"

```sds linenums="69"
```sds linenums="83"
@Pure
@PythonCall("list($this.keys())")
fun keys() -> keys: List<K>
Expand All @@ -67,7 +67,7 @@ Returns the number of entries in the map.

??? quote "Source code in `coreClasses.sdsstub`"

```sds linenums="62"
```sds linenums="76"
@Pure
@PythonCall("len($this)")
fun size() -> size: Int
Expand All @@ -85,7 +85,7 @@ Returns the values of the map.

??? quote "Source code in `coreClasses.sdsstub`"

```sds linenums="76"
```sds linenums="90"
@Pure
@PythonCall("list($this.values())")
fun values() -> values: List<V>
Expand Down
63 changes: 61 additions & 2 deletions docs/stdlib/safeds/lang/String.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,65 @@ Some text.

??? quote "Source code in `coreClasses.sdsstub`"

```sds linenums="84"
class String
```sds linenums="98"
class String {

/**
* Parses the string to a floating-point number.
*/
@Pure
@PythonCall("float($this)")
fun toFloat() -> f: Float

/**
* Parses the string to an integer.
*
* @param base The base of the integer.
*/
@Pure
@PythonCall("int($this, $base)")
fun toInt(base: Int = 10) -> i: Int
}
```

## `#!sds fun` toFloat {#safeds.lang.String.toFloat data-toc-label='toFloat'}

Parses the string to a floating-point number.

**Results:**

| Name | Type | Description |
|------|------|-------------|
| `f` | [`Float`][safeds.lang.Float] | - |

??? quote "Source code in `coreClasses.sdsstub`"

```sds linenums="103"
@Pure
@PythonCall("float($this)")
fun toFloat() -> f: Float
```

## `#!sds fun` toInt {#safeds.lang.String.toInt data-toc-label='toInt'}

Parses the string to an integer.

**Parameters:**

| Name | Type | Description | Default |
|------|------|-------------|---------|
| `base` | [`Int`][safeds.lang.Int] | The base of the integer. | `#!sds 10` |

**Results:**

| Name | Type | Description |
|------|------|-------------|
| `i` | [`Int`][safeds.lang.Int] | - |

??? quote "Source code in `coreClasses.sdsstub`"

```sds linenums="112"
@Pure
@PythonCall("int($this, $base)")
fun toInt(base: Int = 10) -> i: Int
```
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"build:clean": "npm run clean && npm run build",
"watch": "concurrently -n tsc,cli,lang,vscode,eda -c blue,yellow,red,green \"tsc -b tsconfig.json\" \"npm run watch -w=@safe-ds/cli\" \"npm run watch -w=@safe-ds/lang\" \"npm run watch -w=safe-ds\" \"npm run watch -w=@safe-ds/eda\"",
"test": "vitest",
"test-with-coverage": "vitest --coverage"
"test-with-coverage": "vitest --coverage",
"docs:stdlib": "safe-ds document packages/safe-ds-lang/src/resources/builtins -o docs/stdlib"
},
"devDependencies": {
"@lars-reimann/eslint-config-svelte": "^7.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,31 @@ class Boolean
/**
* A number.
*/
class Number {
class Number

/**
* An integer.
*/
class Int sub Number {
/**
* Converts the number to an integer.
* Converts this integer to a floating-point number.
*/
@Pure
@PythonCall("int($this)")
fun toInt() -> i: Int
@PythonCall("float($this)")
fun toFloat() -> f: Float
}

/**
* An integer.
*/
class Int sub Number

/**
* A floating-point number.
*/
class Float sub Number
class Float sub Number {
/**
* Converts this floating-point number to an integer by truncating the fractional part.
*/
@Pure
@PythonCall("int($this)")
fun toInt() -> i: Int
}

/**
* A list of elements.
Expand Down Expand Up @@ -92,9 +98,16 @@ class Map<K, out V> {
class String {

/**
* Converts the string to an integer.
* Parses the string to a floating-point number.
*/
@Pure
@PythonCall("float($this)")
fun toFloat() -> f: Float

/**
* Parses the string to an integer.
*
* @base The base of the integer.
* @param base The base of the integer.
*/
@Pure
@PythonCall("int($this, $base)")
Expand Down

0 comments on commit 55a2050

Please sign in to comment.