import struct


def encode_float(float_, length=None):
	"""
	
	Encodes a floating point value. If length is not None, float_ will be encoded in that many bytes; otherwise, float_ will be encoded in 0 bytes if float_ is None or 0, and 8 bytes in all other cases. If float_ is not None or 0 and length is 0, ValueError will be raised.
	
	:arg float_: the floating point value
	:type float_: float
	:arg length: the length of the encoded representation, or None (defaults to None)
	:type length: int or None
	:returns: the encoded representation bytes
	:rtype: bytearray
	
	"""
	
	if length not in (None, 0, 4, 8):
		raise ValueError('Cannot encode floating point values with lengths other than 0, 4, or 8 bytes.')
	if float_ is None:
		float_ = 0.0
	if float_ == 0.0:
		if length is None:
			length = 0
	else:
		if length is None:
			length = 8
		elif length == 0:
			raise ValueError('Cannot encode floating point value %f as it would have an encoded representation longer than 0 bytes.' % float_)
	
	if length in (4, 8):
		data = bytearray(struct.pack({
			4: '>f',
			8: '>d'
		}[length], float_))
	else:
		data = bytearray()
	
	return data