Invalid value when assigning a variable

Maybe someone sees an error here, I don't see any. The function is null safe, does not make any calculations (no 0 division) and looks like following:

static function formatSpeed(setup, speedInMeterPerSeconds) {
		var settings = System.getDeviceSettings();
	
		var unit = "m/s";
		var speed = speedInMeterPerSeconds;
		
		switch (setup.windSpeedUnit) {
			case 0 /* WIND_SPEED_UNIT_SYSTEM */: {
				if (settings.temperatureUnits == System.UNIT_STATUTE) {
					if (speed != null) {
						speed *= 2.23694; // m/s => miles/hour
					}
					unit = "mph";
				}
				break;
			}
			case 1 /* WIND_SPEED_UNIT_MS */: {
				// passt
				break;
			}
			case 2 /* WIND_SPEED_UNIT_KMH */: {
				if (speed != null) {
					speed *= 3.6;
				}
				unit = "km/h";
				break;
			}
			case 3 /* WIND_SPEED_UNIT_MPH */: {
				if (speed != null) {
					speed *= 2.23694; // m/s => miles/hour
				}
				unit = "mph";
				break;
			}
			case 4 /* WIND_SPEED_UNIT_BEAUFORT */: {
				var bft = 0;
				if (speed == null) {
				} else if (speed < 0.3) {
					// passt
				} else if  (speed < 1.6) {
					bft = 1;
				} else if  (speed < 3.4) {
					bft = 2;
				} else if  (speed < 5.5) {
					bft = 3;
				} else if  (speed < 8.0) {
					bft = 4;
				} else if  (speed < 10.8) {
					bft = 5;
				} else if  (speed < 13.9) {
					bft = 6;
				} else if  (speed < 17.2) {
					bft = 7;
				} else if  (speed < 20.8) {
					bft = 8;
				} else if  (speed < 24.5) {
					bft = 9;
				} else if  (speed < 28.5) {
					bft = 10;
				} else if  (speed < 32.7) {         // <= line 313
					bft = 11;
				} else {
					bft = 12;
				}
				speed = bft;
				unit = "Bft";
				break;                              // <= line 320
			}
			case 5 /* WIND_SPEED_UNIT_KNOTS */: {
				if (speed != null) {
					speed *= 1.94384;
				}
				unit = "kn";
				break;
			}
		}

		if (speed == null) {
			return DataField.EMPTY + unit;
		} else {
			return speed.format("%d") + unit;
		}
	}

I marked line 313 and 320 (line 60 + 67 in the code snipplet) from my source code, in those lines I get following exception: Error Name: Invalid Value

Any ideas how this can happen? As the code above looks save for me, this seems to be something with a misleading stacktrace, but I have no idea where I can look further in this case...